Danial
Danial

Reputation: 703

sending JSON object by needle

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

myProgram API :

Endpoint: http://xxx.yyyy.com/yourCompany-service/rest/myProgram This is a REST service exposed where you need to post a JSON with the following details. here is a sample of a JSON object you can send to the service: Sample Request Json :

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

Now my code:

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 receive following message from server:

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

Upvotes: 3

Views: 4365

Answers (1)

John Sterling
John Sterling

Reputation: 1111

You need to say that you're going to send json to the request method. See the options reference

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

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

Upvotes: 4

Related Questions