HTTP – What does ‘Transfer-Encoding : Chunked’ mean?

Content-Length

The content-length header informs the client of the byte length of the HTTP body. This is typically used for HTTP 1.1 (i.e persistent connections) to ensure the client knows when the current response/request is complete so that the connection can be reused for further requests.

Transfer-Encoding Chunked

However in certain cirucumstances the server may want to start sending the HTTP body back to the client before it actually knows the content-length (i.e because the body is still being generated by the server).

To achieve this the HTTP content-length header is replaced with the HTTP header ‘Transfer-Encoding : Chunked‘ and the response body sent back to the client in chunks.

Each chuck is then constructed starting with the length of current chunk in hexadecimal, then ‘\r\n’, the actual chunk and then finally another ‘\r\n’

Example

Below shows you can example of a chunked response. Here the chunk length is 0x928 which equals 2344 bytes.

HTTP/1.1 200 OK
Date: Thu, 05 Dec 2013 20:13:54 GMT
Server: Server
Pragma: no-cache
x-amz-id-1: 1BBZYT3T0FZ8JEAE59MC
p3p: policyref="http://www.amazon.co.uk/w3c/p3p.xml",CP="CAO DSP LAW CUR ADM IVAo IVDo CONo OTPo OUR DELi PUBi OTRi BUS PHY ONL UNI PUR FIN COM NAV INT DEM CNT STA HEA PRE LOC GOV OTC "
Cache-Control: no-cache
Expires: -1
x-amz-id-2: VIK8CP84Ok1oJo1uxyN8ikt7mZCNNoq4UYNYa/9BuyXs4hCsu0Uk8dr2nwLjN+k3
Vary: Accept-Encoding,User-Agent
Content-Encoding: gzip
Content-Type: text/html; charset=ISO-8859-1
Set-Cookie: ubid-acbuk=279-2585849-5868911; path=/; domain=.amazon.co.uk; expires=Tue, 01-Jan-2036 00:00:01 GMT
session-id-time=2082758401l; path=/; domain=.amazon.co.uk; expires=Tue, 01-Jan-2036 00:00:01 GMT
session-id=280-1566970-1360932; path=/; domain=.amazon.co.uk; expires=Tue, 01-Jan-2036 00:00:01 GMT
Transfer-Encoding: chunked

928

 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">
<html>
  <head>
    ......(omitted)
  </head>
</html>

 

References

http://www.httpwatch.com/httpgallery/chunked/
http://www.codeproject.com/Articles/648526/All-about-http-chunked-responses

 

Rick Donato

Want to become a networking expert?

Here is our hand-picked selection of the best courses you can find online:
Cisco CCNA 200-301 Certification Gold Bootcamp
Complete Cyber Security Course – Network Security
Internet Security Deep Dive course
Python Pro Bootcamp
and our recommended certification practice exams:
AlphaPrep Practice Tests - Free Trial