http post

Posted on
  • I am trying execute a post request from the device, but the device always returns a 'no connection' error when trying to POST.

    To troubleshoot I have ran a GET request to the same URL, and the request was successful, this means that internet is working on the device and communication with the intended server is possible.

    I then moved on to see if there were errors in my HTTP POST code, I ran the same code in a node.js app and it successfully posted to the server.

    Here is the code below, I removed the exact address of my server and my wifi/pass info. I feel as though something is still wrong with the request code.

    var http = require("http");
    var wifi = require("Wifi");
    
    var sdata = {
     deviceID: 'esp-12',
    };
    
    var options = {
      hostname: 'immense-XXXXXX-XXXXX.herokuapp.com',
      method: 'POST',
      path:'/MXXXXXX',
      headers: {
        'Content-Type': 'application/json'
      }
    };
    
    var req = http.request(options, function(res) {
      console.log('Status: ' + res.statusCode);
      console.log('Headers: ' + JSON.stringify(res.headers));
      res.setEncoding('utf8');
      res.on('data', function(body) {
        console.log('Body: ' + body);
      });
    });
    req.on('error', function(e) {
      console.log('problem with request: ' + e.message);
    });
    payload = JSON.stringify(sdata);
    req.write(payload);
    req.end();
    

    terminal response from device after execution

    problem with request: no connection
    

    I know my ESPRUINO device can connect to the wifi because a) I am programming over TCP/IP and the GET requests to the server are successful.

    Here is the documentation for Espruino.js HTTP module. https://www.espruino.com/Reference#http

    Can any of the JS gurus see an issue with the request?

    Sidenote: I have already configured my wifi with wifi.connect(); and wifi.save();

  • All I can think is you need a Content-Length header? It's worth checking out https://www.espruino.com/IoT+Services for some HTTP post examples.

  • That Fixed it!

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

http post

Posted by Avatar for user78438 @user78438

Actions