0

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);
});

}

1 Answer 1

1

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

req.write(postData);
req.end();
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, Trott it works, but i got one another problem of json format my service is replyed with error parsing json, can you help me on that?
If solving this problem uncovered a second problem, post a new question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.