Post JSON to asp web api as request

33 views
Skip to first unread message

Nikos Galanakis

unread,
Feb 11, 2016, 1:15:47 PM2/11/16
to nodejs
Is anyone know how i could send a json .I am trying above way but is not working.Is returning error code 400.But if i copy paste exactly the same the url and the JSON 
to POSTMAN it answering normally. 

var https = require('https');
jsonObject = JSON.stringify({
    (data)
});
var postheaders = {
    'Content-Type' : 'application/json',
    Content-Length' : jsonObject.length
};
var optionspost = {
    host : url,
    port : '',
    path : '/',
    method : 'POST',
    headers : postheaders
};
var reqPost = https.request(optionspost, function(res) {
    console.log("statusCode: ", res.statusCode);
    // uncomment it for header details
//  console.log("headers: ", res.headers);
 
    res.on('data', function(d) {
        console.info('POST result:\n');
        process.stdout.write(d);
        console.info('\n\nPOST completed');
    });
});
 
// write the json data
reqPost.write(jsonObject);
reqPost.end();
reqPost.on('error', function(e) {
    console.error(e);
});

Ryan Schmidt

unread,
Feb 14, 2016, 2:33:31 PM2/14/16
to nod...@googlegroups.com

On Feb 11, 2016, at 10:10 AM, Nikos Galanakis wrote:

> Is anyone know how i could send a json .I am trying above way but is not working.Is returning error code 400.But if i copy paste exactly the same the url and the JSON
> to POSTMAN it answering normally.

Perhaps you are using application/x-www-form-urlencoded and should instead use multipart/form-data, or vice versa? Some servers may only support one or the other.

Reply all
Reply to author
Forward
0 new messages