How to request HTTPS using http.request

Posted on
  • In the docs I can see there is provision for HTTPS when using http.get, you just pass a https:// url and it works it out. But what about when using http.request?

      var options = {
        host: 'google.com',
        port: 80,
        path: '/',
        method: 'GET',
      };
    
      console.log('about to http request');
      http.request(options, function(res) {
        console.log('request cb');
        res.on('data', function(data) {
          console.log("HTTP> "+data);
        });
    
        res.on('close', function(data) {
          console.log(data);
          console.log("Connection closed");
        });
    
        res.on('error', function(err) {
          console.log(err);
        });
      }).end();
    

    Works fine, but if I change the port to 443 it just stalls. I assume because its trying to talk HTTP to a HTTPS port. I don't really want to use tls.connect because that gives me a socket rather than a https connection.

  • Got it, you need to specify an undocumented parameter in options called protocol, and set it to https: (note the trailing colon).

    var options = {
        host: 'google.com',
        protocol: 'https:',
        port: 443,
        path: '/',
        method: 'GET',
      };
    
  • Thanks - I've just updated the docs, so it'll be on the main site next time I update

  • I want to make a PUT HTTPS request with the Espruino WiFi module. How can I do this? Require("https") is not recognised. Can someone give me an example code?

  • Hi!

    You can try something like this:

    // Load the WiFi and HTTP modules
    var wifi = require("Wifi");
    var https = require("http");
    
    // Your WiFi credentials
    var SSID = "your-SSID";
    var PASSWORD = "your-password";
    
    // Connect to WiFi
    wifi.connect(SSID, { password: PASSWORD }, function(err) {
      if (err) {
        console.log("WiFi connection error: ", err);
        return;
      }
      console.log("Connected to WiFi");
    
      // Options for the HTTPS request
      var options = {
        host: "your-server.com",
        port: 443,
        path: "/your-endpoint",
        method: "PUT",
        headers: {
          "Content-Type": "application/json",
          "Content-Length": JSON.stringify({ data: "your-data" }).length
        }
      };
    
    
  • Hello,

    I succeeded in making an HTTPS request but after a few attempts, I get the messag below. Anyone who can help me with this?


    1 Attachment

    • Faultcode.JPG
  • Sorry to hear that - that could be a sign that you're running low on memory as HTTPS can take a lot.

    Do you think it's possible that maybe you're not closing the HTTPS connection, so it's staying open? So then multiple HTTPS connections open at the same time would chew through your RAM.

    You could check process.memory().usage to see if the free memory is going down after each call, or if it's staying constant (which is what you want really)

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

How to request HTTPS using http.request

Posted by Avatar for dave_irvine @dave_irvine

Actions