I have a (GET) endpoint that sends data in chunks (Transfer-Encoding: chunked
). The data is JSON encoded and sent line by line.
Is there a way to consume the data sent by this endpoint in an asynchronous manner in JavaScript (or using some JavaScript library)?
To be clear, I know how to perform an asynchronous GET
, but I would like to have the GET
request not waiting for the whole data to be transfered, but instead read the data line by line as it arrives. For instance, when doing:
curl http://localhost:8081/numbers
The lines below are shown one by one as they become available (the example server I made is waiting a second between sending a line and the second).
{"age":1,"name":"John"}
{"age":2,"name":"John"}
{"age":3,"name":"John"}
{"age":4,"name":"John"}
I would like to reproduce the same behavior curl
exhibits, but in the browser. I don't want is leave the user wait till all the data becomes available in order to show anything.
ReadableStream
a try. – RadiographTypeError: response.body is undefined
). – RadiographTransfer-Encoding: chunked
stream format. It's missing the chunk length. – Amnesia