Sending a JSON object with a needle

I am trying to send a JSON object to a URL (provided) by someone else. Here is the documentation provided by support:

MyProgram API:

Endpoint: http://xxx.yyyy.com/yourCompany-service/rest/myProgram This is a REST service where you need to put JSON with the following details. here is a sample JSON object that you can send to the service: Example Json request:

{
    "aa": "ertewer",
    "bb": 1,
    "cc": 10
}

      

Now my code is:

var options = {
    host : 'http://xxx.yyyy.com/yourCompany-service/rest/myProgram',
    format : 'json',
    content_type: 'application/json'

} ;
var x = {
               "aa"       : "ABCD",
               "bb"        : 1,
               "cc"     : 10,
       }

needle.request('post', options.host, x, function(err, resp) {
           if (!err) {
               console.log(resp.body) ;
           }

           if (err) {
               console.log('neddle error');
           }
 }

      

but I always get the following message from the server:

The server rejected this request because the request object is in a format not supported by the requested resource for the request

+3


source to share


1 answer


You need to be told that you are going to send json to the request method. See Link



needle.request('post', options.host, x, {json:true}, function(err, resp) {
           if (!err) {
               console.log(resp.body) ;
           }

           if (err) {
               console.log('neddle error');
           }
 }

      

+3


source







All Articles