0

The previous answer requires the request component, of which I like to avoid using, due to academic purpose and other related policy. With vanilla Node.js 8.4.0, I tried:

var https = require('https');
var sendData = {
  api_key: 'abc',
  api_secret: '123',
  image_url: 'http://lalala.com/123/lalala.jpg',
  return_attributes: ['gender','age']
};
var options = {
  hostname: 'lalala.com',
  port: '443',
  path: '/info',
  method: 'POST',
  rejectUnauthorized: false,
  requestCert: true,
  headers: {
    'Content-Type': 'application/json',
  }
};
var openreq = https.request(options, function(serverFeedback){
  if (serverFeedback.statusCode == 200) {
    var body = '';
    serverFeedback.on('data', (data)=>{ body += data; })
      .on('end', ()=>{
        console.log(body);
      });
  } else {
    console.log('failed');
  }
});
openreq.write(JSON.stringify(sendData))
openreq.end();

Sadly the code above results in failed output.

Aero Wang
  • 7,246
  • 11
  • 45
  • 83
  • you want to send JSON to web server? You will have to use node modules like, `https`, `request` in order to do that, also you can put `data` inside `options` and the program will send the data inside `req.body` as a result of a post request – turmuka Sep 05 '17 at 09:37
  • 1
    Add error handler: `openreq.on('error', (e) => { console.error(e); });` to know what is the error reason. – alexmac Sep 05 '17 at 09:40
  • 1
    So what is `serverFeedback.statusCode`? – robertklep Sep 05 '17 at 09:45
  • put some error handlers – turmuka Sep 05 '17 at 09:47
  • You are not sending `Content-Length` in `options.headers`. No wonder the server rejects the request. Stringify `sendData`, then set `Content-Length` header and then send `sendData`. – freakish Sep 05 '17 at 10:00
  • @Aero Wang: Did you solve your issue? – Damaged Organic Sep 06 '17 at 17:39
  • @KidBinary I haven't had time to try. – Aero Wang Sep 07 '17 at 02:57
  • @Aero Wang: I see. Please, if you'll find the reason behind this issue, give community some feedback - I'm as well interested in what was wrong in that request. – Damaged Organic Sep 07 '17 at 08:48
  • @KidBinary since I just changed my server to accept form data instead of json so I didn't test it out but I marked it as the correct answer anyways. Someone else plz verify this. – Aero Wang Sep 18 '17 at 05:02

1 Answers1

1

There is nothing wrong with your request flow, as it almost exactly resembles Node.js documentation HTTP and HTTPS guides (apart from JSON Content-Type). However, you're only looking for 200 response and not expecting that error could have message in body that should be captured before serverFeedback.statusCode == 200 condition:

serverFeedback.setEncoding('utf8');
serverFeedback.on('data', (chunk) => {
  console.log(`BODY: ${chunk}`);
});
serverFeedback.on('end', () => {
  console.log('No more data in response.');
});

In any case, problem is definitely with the remote host then, and you could also observe response information more closely with:

console.log(`STATUS: ${serverFeedback.statusCode}`);
console.log(`HEADERS: ${JSON.stringify(serverFeedback.headers)}`);

Just one more thing, if you're using version 8, for the same academic purposes it's worth reconsidering utilization of var in favor of let & const, as well as fat arrow functions and Promises instead of callbacks.

Damaged Organic
  • 7,639
  • 5
  • 56
  • 81