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!

  • How you fix this? I have the same problem with you

  • Hi there! I'm trying to get an example on how to make a post to io.adafruit.com from espruino with a NodeMCU and I couldn't find any example. Is there a way to pass the authentication headers?

  • @user87799 headers won't be a problem, I don't think, but ESP8266 on the NodeMCU is not capable of HTTPS communication via Espruino, so unless Adafruit offer the http:// schema on their API you'll need use a Pico or Espruino Wifi (or maybe ESP32 - I'm not too familiar).

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

http post

Posted by Avatar for user78438 @user78438

Actions