简体   繁体   中英

Unable to POST data in node.js script

I am requesting a https post method with a data but my data is not going with the request and thats why service is giving error, so how can i change my code so that it will get to the server , here is my code

var https = require('https');
var querystring = require('querystring');
connect();
function connect(){

var postData = querystring.stringify({
  'application': 'QF2',
    'client': 'COMMAND',
    'userId': 'devicexxx',
    'operation': 'at-cmd',
    'payload': 'dfdfdfdf',
    'messageId': '123e454567-e89b-12d3-a456-42665544'
});
var options = {
  hostname: 'cus.inco.com',
  port: 443,
  path: '/portal/action/dev',
  method: 'POST',
  rejectUnauthorized: false,
  headers: {
    'Content-Type': 'application/json',
    'Content-Length': postData.length
  }
};

var req = https.request(options, function(res) {
  console.log("statusCode: ", res.statusCode);
  //console.log("headers: ", res.headers);
    res.setEncoding('utf8');
  res.on('data', function(d) {
    console.log(d)
  });
});
req.end();

req.on('error', function(e) {
  console.error(e);
});

}

You need to write() your POST data before you end the request.

req.write(postData);
req.end();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM