简体   繁体   中英

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://

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.

You can use axios

var axios = require('axios');

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM