Tanoy B
Tanoy B

Reputation: 41

Make https request from Node js http server

My live site is https://example.com. Now I've created a node js server on http://example.com:8081. I've following code:

var options = {
    host: 'https://example.com',
    port: 443,
    path: '/scanner/scoutdata',
    method: 'GET'
};
and sending request like this:
http.request(options, function(res){
  //my code here.
});

Now I'm getting a 302 error message when I run this script in linux server. Can't I call a url with https:// from a http:// node server under the same domain? Should I create my node js server in https:// to call urls with https://

Upvotes: 1

Views: 4077

Answers (3)

Gaurav Mogha
Gaurav Mogha

Reputation: 400

You can use axios

var axios = require('axios');

axios.get("https://example.com:443/scanner/scoutdata").then(function (response) {
    console.log(response.data);
  });

Upvotes: 0

jfriend00
jfriend00

Reputation: 707158

Use https.get() instead of http.get() if contacting a server on port 443 (the SSL port).

The 302 response is just telling you that the port and protocol are not matching and it expects them to match. So, use http on port 80 and https on port 443.

Upvotes: 2

Dineshaws
Dineshaws

Reputation: 2083

Please try with request module as i am putting code below-

request("https://example.com:443/scanner/scoutdata", function(error, response, body) { 
       console.log(body);
});

Thanks

Upvotes: -1

Related Questions