2

I'm trying to send a POST request to an endpoint which accepts JSON and it doesn't work. Do I have to send any specific parameter in order to let the network know it is encoded as JSON?

Here is the simple request I've so far:

var request = require('request')

var cookie = '**Here the cookie copied from the Network tab from the Chrome Dev Tools Bar**'
var UA = '**Here the UA copied from the Network tab from the Chrome Dev Tools Bar**'

var JSONformData = {"jsonrpc":"2.0","method":"LMT_split_into_sentences","params":{"texts":["Text"],"lang":{"lang_user_selected":"auto","user_preferred_langs":["EN","ES"]}},"id":8}

var URL = 'https://www.deepl.com/jsonrpc'

request.cookie(cookie)
request.post({
        url: URL, 
        headers: {
            'User-Agent': UA
        },
        form: JSONformData
    }, function(error, response, body) {
        console.log(response)
    }
)

1 Answer 1

6

If you are sending JSON data then you don't need to specify the form, instead specify the json for data in the options object:

request.post({
        url: URL, 
        headers: {
            'User-Agent': UA
        },
         json: JSONformData
    }, function(error, response, body) {
        console.log(response)
    })
Sign up to request clarification or add additional context in comments.

4 Comments

You must be doing something wrong in your code. Check the docs here: github.com/request/request#requestoptions-callback Specifying Json data with json key will set the content type to json and body to the json data.
As you can see here: github.com/request/request#forms body: JSON.stringify(...) That's the point
What's the point in that? He wanted to send Json data with request module and that's how the docs specify it's done
@ÓscarAndreu When you use body: JSON.stringify you need to set content-type to application/json. But when you set data to json field, request sets it automatically.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.