HTTPS request in NodeJS

I am trying to write a NodeJS application that will talk to the OpenShift REST API using the request method in the https package. Here is the code:

var https = require('https'); var options = { host: 'openshift.redhat.com', port: 443, path: '/broker/rest/api', method: 'GET' }; var req = https.request(options, function(res) { console.log(res.statusCode); res.on('data', function(d) { process.stdout.write(d); }); }); req.end(); req.on('error', function(e) { console.error(e); }); 

But it gives me an error (status code 500 is returned). When I did the same using curl on the command line,

 curl -k -X GET https://openshift.redhat.com/broker/rest/api 

I get the correct response from the server.

Is there something wrong in the code?

+46
rest openshift
Oct 12
source share
1 answer

Comparing what the headers and node sent, I found that the addition:

 headers: { accept: '*/*' } 

to options fixed.




To find out which bounce headers are being sent, you can use the -v argument.
curl -vIX GET https://openshift.redhat.com/broker/rest/api

In node, just console.log(req._headers) after req.end() .




Quick tip: you can use https.get() instead of https.request() . It will set the GET method and call req.end() for you.

+45
12 Oct
source



All Articles