My goal is to write a script that sets up an HTTP server and when I post data in from a client, I want to see that data within Espruino. To that end, I set up the following script:
var wifi = require("Wifi");
var esp8266 = require("ESP8266");
var http = require("http");
wifi.startAP("ESP8266", {
"authMode": "open"
}, function(err) {
var httpServer = http.createServer(function(request, response) {
console.log("Received a server request!" + JSON.stringify(request));
if (request.available() === 0) {
console.log("No data");
} else {
var receivedData = request.read();
console.log("Data = " + receivedData);
}
response.write("Response data!");
response.end();
});
httpServer.listen(80);
});
when the request is received by the ESP8266, the message logged is:
Received a server request!{"type":1,"res":{},"svr":{"type":1,"port":80,"sckt":9},"sckt":10,"headers":{"User-Agent":"Wget/1.16 (linux-gnueabihf)","Accept":"*/*","Host":"192.168.4.1","Connection":"Keep-Alive","Content-Type":"application/x-www-form-urlencoded","Content-Length":"5"},"method":"POST","url":"/hello","hdrs":true,"dRcv":""}
No data
From this note two things. The first is that the amount of available bytes is flagged as 0 while the HTTP header says that 5 bytes (what I would expect) was sent.
I do in fact see the data. What is puzzling me is the notion that I had expected available() to return 5 for the amount of payload data and response.read() to be able to read that data. Can anyone spot what I might have mis understood?
Espruino is a JavaScript interpreter for low-power Microcontrollers. This site is both a support community for Espruino and a place to share what you are working on.
My goal is to write a script that sets up an HTTP server and when I post data in from a client, I want to see that data within Espruino. To that end, I set up the following script:
I then send in a POST request using:
when the request is received by the ESP8266, the message logged is:
From this note two things. The first is that the amount of available bytes is flagged as 0 while the HTTP header says that 5 bytes (what I would expect) was sent.
If I change the code to include:
I do in fact see the data. What is puzzling me is the notion that I had expected
available()
to return 5 for the amount of payload data andresponse.read()
to be able to read that data. Can anyone spot what I might have mis understood?