Problems with POST through HTTP.request

Posted on
  • Hi Guys - having issues getting an HTTP POST to work. Device is an espruino wifi, flashed to the latest version.

    Here's the code:

    var WIFI_NAME = "xxxx";
    var WIFI_OPTIONS = { password : "yyy" };
    var wifi = require("EspruinoWiFi");
    
    wifi.connect(WIFI_NAME, WIFI_OPTIONS, function(err) {
      if (err) {
        console.log("Connection error: "+err);
        return;
      }
      console.log("Connected!");
      sendData();
      
    });
    
    function sendData() {
      var options = {
        host: 'http://192.168.1.66', // host name
        port: 8081,            // (optional) port, defaults to 80
        path: '/stats/login/',           // path sent to server
        method: 'POST',
        headers: { "Content-type" : "application/json" } 
      };
      
      
      var req = require("http").request(options, function(res) {
        
        console.log('res',res);
        
         res.on('error', function(data) {
          console.log("error> "+data);
        });
        
        res.on('data', function(data) {
          console.log("HTTP> "+data);
        });
        res.on('close', function(data) {
          console.log("Connection closed");
        });
      });
      
      req.end("{module:'espruino'}");  
      console.log("Request sent"); 
    

    The connection works OK and I also get the 'request sent' message, but nothing after that on the espruino, and no connection received by the server.

    I've verified that the endpoint (which is a server of mine) is working OK (accepts a POST from a REST client), and HTTP.GET works fine (copied one of the examples). I'm doubtless doing something basic wrong, but not sure how to debug further...

  • That's odd... Could you have a go at adding a 'Content-Length' header?

    There are a few examples of POSTing data on the IoT services page: http://www.espruino.com/IoT+Services

    and you could have a go at modifying one of those?

  • OK - so I got the dweet one working OK, but still no luck with mine (including the content-length). Is there any way to see the actual request being made? The request function is never invoking the response callback, so would be nice to se what was on the wire...

    cheers

  • Change your host from host: 'http://192.168.1.66', to host: '192.168.1.66', and give that a try.

  • Wow, nicely spotted @dave_irvine! :)

    You can add an error handler to see what's up with the connection as well if you want to: http://www.espruino.com/Internet#handlin­g-errors

  • Nicely spotted indeed! All working now - thanks...
    For my future reference here's the final code that works (note that the res.on() events are only triggered if the server actually returns something:

    var WIFI_NAME = "";
    var WIFI_OPTIONS = { password : "" };
    var wifi = require("EspruinoWiFi");
    
    wifi.connect(WIFI_NAME, WIFI_OPTIONS, function(err) {
      if (err) {
        console.log("Connection error: "+err);
        return;
      }
      console.log("Connected!");
      sendData();
    });
    
    function sendData() {
      var content = "{module:'espruino'}";
      var options = {
        host: '192.168.1.66', // host ip (host name works as well)
        port: 8081,            // (optional) port, defaults to 80
        path: '/stats/login/',           // path sent to server
        method: 'POST',
        headers: { "Content-type" : "application/json",
                 "Content-length": content.length} 
      };
      
      var req = require("http").request(options, function(res) {
        console.log('res',res);
        res.on('data', function(data) {
          console.log("HTTP> "+data);
        });
        res.on('close', function(data) {
          console.log("Connection closed");
        });
      });
      
      req.on('error',function(err){
        console.log(err);
      });
      
      req.end(content);  
      console.log("Request sent");  
    }
    
  • Was content-length required in the end? Or was it just the URL that @dave_irvine spotted?

  • It was the URL - adding the content-length didn't help...

  • hey mr @davidhay. I am trying your code out and I have replaced the host and wifi info with my own, but for some odd reason i am getting a code-6 message not found

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

Problems with POST through HTTP.request

Posted by Avatar for davidhay @davidhay

Actions