Reading the complete content of an HTTP GET request #470
Replies: 4 comments
-
Posted at 2015-10-26 by tve The client does not close an HTTP connection after sending the request. Specially not with HTTP 1.1. You have to check whether there's a Content-Length header and if there is, read that number of bytes from the socket as payload. (If the encoding is chunked you have to do the equivalent using the chunk length prefixes.) |
Beta Was this translation helpful? Give feedback.
-
Posted at 2015-10-26 by @gfwilliams Espruino's HTTP GET does close the socket right after requesting a page (it tests via content length)... Adding the ability to leave the socket open to request more stuff just seemed like overkill. Can you not just do:
That's what is shown in all the examples... But you're saying that if you don't have a data handler, you don't get Thing is, even if it worked, doing what you're doing would be a really bad idea - so as not to fill up Espruino's memory if you're not reading data, serial/sockets/etc only buffer ~500 bytes worth of data, and chuck away the rest. You're best off having a data handler, and manually saving away the data as it arrives. |
Beta Was this translation helpful? Give feedback.
-
Posted at 2015-10-26 by Kolban What I'm trying to do is "play" end user. So I go to the API docs for http://www.espruino.com/Reference#l_httpSRq_read The few words here say that when Then I pair that with the I am sensing that this is not a valid assumption ... and if that is the case, what I'd like to do is document that consideration so that future travelers won't be led down this path. |
Beta Was this translation helpful? Give feedback.
-
Posted at 2015-10-26 by @gfwilliams Yes, sounds like a good plan. However if you look at the docs for It's presumably just if you look at the Reference page itself for HttpCRs (in isolation) while ignoring the code under |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Posted at 2015-10-26 by Kolban
In this puzzle, my goal is to issue an HTTP GET request and when the complete response has arrived, process it.
Looking at the APIs, I see:
In the response handler, I am given an object of type
httpCRs
(I believe). However, if I try and print:I am returned a value of 0. I figured that was ok, because at this point, in execution, data had not yet arrived and there was indeed none available.
So I figured that I could code up the following:
However something seems to be wrong here. The "close" event function is not being called. By sheer luck, I found that if I coded:
I then find that the code event callback is being invoked ... but of course, I have consumed my data.
So it seems that if I code a
response.on("close"...)
without aresponse.on("data"...)
the close callback is not being invoked.Am I missing a concept here?
Beta Was this translation helpful? Give feedback.
All reactions