3

I am sendin a request from node server to some other server , but i need to send content type

application/json

How can i send that , I am using this format

request.post('https://server.com/index.php/rest/V1/integration/admin/token',{form:postData},function (error, response, body) {
    console.log('error:', error); // Print the error if one occurred
    console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received
    console.log('body:', body); // Print the HTML for the Google homepage.
    res.json({
        'error': error,
        'statusCode': response && response.statusCode,
        'body': body
    })
});

I am getting error while I am trying this

 request.post(
        'https://server.com/index.php/rest/V1/integration/admin/token',
        {
            form:postData,
            headers:{ 
                "Content-Type": "application/json"
            }

    },function (error, response, body) {
        console.log('error:', error); // Print the error if one occurred
        console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received
        console.log('body:', body); // Print the HTML for the Google homepage.
        res.json({
            'error': error,
            'statusCode': response && response.statusCode,
            'body': body
        })
    });

"message":"Server cannot understand Content-Type HTTP header media type application/x-www-form-urlencoded"

Ashutosh Jha
  • 15,451
  • 11
  • 52
  • 85

4 Answers4

8

Please change key name form to json.

    request.post('https://server.com/index.php/rest/V1/integration/admin/token', {
    json: postData
}, function(error, response, body) {
    console.log('error:', error); // Print the error if one occurred
    console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received
    console.log('body:', body); // Print the HTML for the Google homepage.
    res.json({
        'error': error,
        'statusCode': response && response.statusCode,
        'body': body
    })
});
Manjeet Thakur
  • 2,288
  • 1
  • 16
  • 35
3

As per the docs you need to set json:true for application/json to be added to the header

let data = { foo: "bar" }

request.post({
    uri: 'www.domain.com/upload',
    body: data,
    json:true
}, function (err: any, res: any, body: any) {
    //handle callback            
});
wal
  • 17,409
  • 8
  • 74
  • 109
-1

See the documentation. You just need to include a value for the headers property in the options object:

request.post(
    'https://server.com/index.php/rest/V1/integration/admin/token',
    {
        form: postData, /* Ensure this is a string of JSON! */
        headers:{ 
            "Content-Type": "application/json"
        }
    },
    your_callback_function
);
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
-1

Fixed by this ,

request.post({
    url: url,
    method: "POST",
    headers: {
        "content-type": "application/json",
    },
    json: postData
},function (error, response, body) {
    console.log('error:', error); // Print the error if one occurred
    console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received
    console.log('body:', body); // Print the HTML for the Google homepage.
    res.json({
        'error': error,
        'statusCode': response && response.statusCode,
        'body': body
    })
});
Ashutosh Jha
  • 15,451
  • 11
  • 52
  • 85