Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTTPS request in NodeJS

I am trying to write a NodeJS app which 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 this is giving me an error (status code 500 is returned). When I did the same thing using curl on the command line,

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

I am getting the correct response from the server.

Is there anything wrong in the code?

like image 751
ssb Avatar asked Oct 12 '12 03:10

ssb


People also ask

Is HTTPS built into Node?

To create an HTTPS server, you need two things: an SSL certificate, and built-in https Node.

How do I send a Node.js server request?

Server (Node.js)var server = http. createServer(function (request, response) { var queryData = url. parse(request. url, true).


1 Answers

Comparing what headers curl and node sent, i found that adding:

headers: {     accept: '*/*' } 

to options fixed it.


To see which headers curl sends, 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 method to GET, and calls req.end() for you.

like image 175
MiniGod Avatar answered Sep 16 '22 16:09

MiniGod