Cannot POST with ESP8266 (espruino)

183 Views Asked by At

I cannot make post request (get works fine) with espruino. I've already checked the documentation and it seems pretty equal here is my code:

let json = JSON.stringify({v:"1"});

let options = {
    host: 'https://******,
    protocol: 'https',
    path: '/api/post/*****',
    method: 'POST',
    headers:{
      "Content-Type":"application/json",
      "Content-Length":json.length
    }
  };

let post = require("http").request(options, function(res){
  res.on('data', function(data){
    console.log('data: ' + data);
  });
  res.on('close', function(data){
    console.log('Connection closed');
  });
});

post.end(json);

The espruino console only return the 'connection closed' console.log. The node.js server console (hosted on heroku and tested with postman) dont return anything. Obv the esp8266 is connected to the network

2

There are 2 best solutions below

0
On BEST ANSWER

What you're doing looks fine (an HTTP Post example is here), however Espruino doesn't support HTTPS on ESP8266 at the moment (there isn't enough memory on the chips for JS and HTTPS).

So Espruino will be ignoring the https in the URL and going via HTTP. It's possible that your server supports HTTP GET requests, but POST requests have to be made via HTTPS which is why it's not working?

If you did need to use HTTPS with Espruino then there's always the official Espruino WiFi boards, or I believe ESP32 supports it fine too.

0
On

you're using a package called "http" and then trying to send a request over https. You should also log out 'data' in the res.close so you can get some errors to work with.