I am trying to understand about chunks in HTTP request. As I understood if I want to send chunks I need to put the following header:
Transfer-Encoding: chunked
I saw an example in https://en.wikipedia.org/wiki/Chunked_transfer_encoding
4\r\n (bytes to send)
Wiki\r\n (data)
6\r\n (bytes to send)
pedia \r\n (data)
E\r\n (bytes to send)
in \r\n
\r\n
chunks.\r\n (data)
0\r\n (final byte - 0)
\r\n (end message)
how the chunks works behind the scene ? From what I understand this are multiple http requests using keep-alive, but what I see here in the above example is one blob .
Is this the correct way I need to send chunks ?
First http like this:
4\r\n (bytes to send)
Wiki\r\n (data)
Second http:
6\r\n (bytes to send)
pedia \r\n (data)
Last http:
6\r\n (bytes to send)
AAAAAA \r\n (data)
0\r\n (final byte - 0)
\r\n (end message)
I will be glad if you can show me an example in python.I have looked in many examples on the internet.
As far as I understand, chunked encoding is used to stream data with HTTP over a TCP connection. With this header, you declare that you do not know ahead of time the size of the content that you will send. That way you can start sending content as soon as you have some. However, I do not think it creates multiple HTTP requests which each chunk, there is one single potentially long lived HTTP request (or response).
As for the python code to generate chunks, you can have a look at this SO post Python3 ThreadingHTTPServer fails to send chunked encoded response