• 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);
    });
    

    I then send in a POST request using:

    wget http://192.168.4.1/hello  --post-data="hello" --quiet --output-document=-
    

    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,"header­s":{"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"},"metho­d":"POST","url":"/hello","hdrs":true,"dR­cv":""}
    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.

    If I change the code to include:

    request.on("data", function(data) {
       console.log(data);
    });
    

    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?

About

Avatar for Kolban @Kolban started