You are reading a single comment by @Schweigi and its replies. Click here to read the full conversation.
  • Ups. I forgot to call req.end() when I created the test code for the GET request. I'm sorry for the confusion. So the following is working too now:

    var options = { host: "http://www.google.com" };
    var req = require("http").request(options, function(res)  {
            console.log("status: " + res.statusCode);
    });
    req.end(); // <=== was missing
    

    But I still have some issues with the post call not ending. I think its because of the Mandrill API. The following sample code (POST req. to Requestbin) is working. The console prints the status code (200) after the request:

    Serial1.setup(9600, { rx: B7, tx : B6 });
    var wifi = require("ESP8266WiFi").connect(Serial1, function(err) {
      if (err) throw err;
    
      wifi.reset(function(err) {
        if (err) throw err;
    
        wifi.connect("<WIFI_SSID>", "<WIFI_PW>", function(err) {
          if (err) throw err;
    
          var content = JSON.stringify({ "msg": "Hello World!"});
          var options = { 
            host: "requestb.in", 
            port: 80, 
            path: "/1lxvhjs1", 
            method: "POST",
            headers: {
              "Content-Type": "application/json",
              "Content-Length": content.length
            }
          };
          var req = require("http").request(options, function(res)  {
              console.log("status: " + res.statusCode);
          });
          req.end(content);
          console.log('Request sent');
        });
      });
    });
    

    But when I do the same for the Mandrill API then the post request is not ending. Mandrill gets the request successfully but its seems that the Espruino is still waiting for a response:

    Serial1.setup(9600, { rx: B7, tx : B6 });
    var wifi = require("ESP8266WiFi").connect(Serial1, function(err) {
      if (err) throw err;
    
      wifi.reset(function(err) {
        if (err) throw err;
    
        wifi.connect("<WIFI_SSID>", "<WIFI_PW>", function(err) {
          if (err) throw err;
          
          var content = JSON.stringify({
            "key": "<MANDRILL_API_TOKEN>",
            "text": "This is a demo",
            "message": {
              "from_email": "example@mandrillapp.com",
              "to": [
                {
                  "email": "<YOUR_EMAIL>",
                  "type": "to"
                }
              ]
            }   
          });
          
          var options = { 
            host: "mandrillapp.com", 
            port: 80, 
            path: "/api/1.0/messages/send.json", 
            method: "POST",
            headers: {
              "Content-Type": "application/json",
              "Content-Length": content.length
            }
          };
          var req = require("http").request(options, function(res)  {
              console.log("status: " + res.statusCode);
          });
          req.end(content);
          console.log('Request sent');
        });
      });
    });
    

    I will try to investigate further. The same request (http & https) using a rest-client (Postman) is working correctly.

About

Avatar for Schweigi @Schweigi started