How can I return datas to Http Request?

Posted on
  • Now I use a SIM7600CE for 4G connection. The modules didn't adapt for this kind of sim card.
    So I have to write my own module for it.

    Now I can get response datas in this Method:

    recv: function (sckt, maxLen) {
        if (sockData[sckt]) {
          var r;
          console.log("recv", sockData[sckt].length);
          if (sockData[sckt].length > maxLen) {
            r = sockData[sckt].substr(0, maxLen);
            sockData[sckt] = sockData[sckt].substr(maxLen);
            console.log("recv", sockData[sckt]);
          } else {
            r = sockData[sckt];
            sockData[sckt] = "";
            console.log("recv | socks[sckt]", socks[sckt]);
            if (socks[sckt] == "DataClose") {
              dbg("Got DataClose - forcing close");
              socks[sckt] = "closing";
            }
          }
          console.log("[recv data]", r)
          return r;
        }
        if (socks[sckt] == "closing" || !socks[sckt]) return -1; // close it
        return "";
      },
    

    I think this part is the place where return datas to http.get().
    But I can't recieve any datas in this place.
    Do you have any introductions about how the datas flow in the network?

    http.get("http://www.pur3.co.uk/hello.tx­t", function (res) {
          let str = ''; 
          res.on('data', chunk => {
            console.log("chunk",chunk);
            str += chunk;
          });
          res.on('end', () => {
            console.log("result: --->   ", str);
          });
        });
    
  • Tue 2020.03.24

    Has an attempt to place a console.log() statement just after the http.get request at L1a to see if anything is being detected/returned within the callback?
    or use of the debugger?

    Related content with fix

    forum: http.request not working on Espruino Wifi

    Could it be related to https

    https://www.espruino.com/Internet#https

    http://www.espruino.com/Internet

  • The code

    console.log("back from network?",res)
    didn't print anything.

    tp.get("http://www.pur3.co.uk/hello.txt"­, function (res) {
         console.log("back from network?",res)
          let str = ''; 
          res.on('data', chunk => {
            console.log("chunk",chunk);
            str += chunk;
          });
          res.on('end', () => {
            console.log("result: --->   ", str);
          });
        });
    

    In the Mehtod recv(), I did got datas and retruned them. But where these datas go I really don't know, and how do they go back to http.get response body?

    console.log("[recv data]", r)

    return r;
    
  • This might have the solution and a reasonable explanation:

    'On the Internet page there is a simple example of sending data in the URL from a webpage, but it can be unreliable if anywhere between your browser and the Espruino decides to cache the received data.'

    http://www.espruino.com/Posting+Forms



    My suggestion is to post the response data in it's entirety so that others may take a look and compare to what should be in the response packet.


    'how do they go back to'

    Don't know but, could it be the use of the arrow functions not allowing binding to the http response object?

  • This is the request message

    GET /hello.txt HTTP/1.1
    User-Agent: Espruino 2v04.401
    Connection: close
    Host: http://www.pur3.co.uk

    And this is the response message

    RECV FROM:93.93.135.13:80
    +IPD259
    HTTP/1.1 200 OK
    Date: Wed, 25 Mar 2020 02:30:54 GMT
    Server: Apache/2.4.18 (Ubuntu)
    Last-Modified: Fri, 15 Nov 2013 15:42:26 GMT
    ETag: "d-4eb390b887c80"
    Accept-Ranges: bytes
    Content-Length: 13
    Connection: close
    Content-Type: text/plain
    Hello World!
    +IPCLOSE: 0,1

    I tried to put debuggers in two places. In this place, I can't reach the debugger 2.

         debugger;  // 1
        http.get("http://www.pur3.co.uk/hello.tx­t", function(res) {
          debugger;  //2
          let str = ''; 
          res.on('data', chunk => {
            console.log("chunk",chunk);
            str += chunk;
          });
          res.on('end', () => {
            console.log("Get some test request contents: --->   ", str);
          });
        });
    

    And the second place here:

    recv: function (sckt, maxLen) {
        if (sockData[sckt]) {
          console.log("ddddddddddddddd",sockData[s­ckt])
          var r;
          console.log("recv", sockData[sckt].length);
          if (sockData[sckt].length > maxLen) {
            r = sockData[sckt].substr(0, maxLen);
            sockData[sckt] = sockData[sckt].substr(maxLen);
            console.log("recv", sockData[sckt]);
          } else {
            r = sockData[sckt];
            sockData[sckt] = "";
            console.log("recv | socks[sckt]", socks[sckt]);
            if (socks[sckt] == "DataClose") {
              dbg("Got DataClose - forcing close");
              socks[sckt] = "closing";
            }
          }
          console.log("[recv data]", r);
          debugger; // 3
          return r;
        }
        if (socks[sckt] == "closing" || !socks[sckt]) return -1; // close it
        return "";
      },
    

    I can reach debugger 3. And then where the code goes puzzle me so much! What's the relation between Http.get() and recv()?

    debug>step

              return r;
              ^
    

    debug>step
    Value returned is ="Hello World!"

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

How can I return datas to Http Request?

Posted by Avatar for user109783 @user109783

Actions