Intermediate LevelREST APIs & Web Services
How does Transfer-Encoding: chunked stream data without knowing content length?
❓ Interview Prompt: Explain `Transfer-Encoding: chunked`, dynamic streaming responses, hex size prefixes, and eliminating `Content-Length` header requirements.
When generating large API responses dynamically (e.g. exporting 500,000 database rows), calculating the total byte size beforehand forces the server to buffer all data in RAM to set 'Content-Length':
'Transfer-Encoding: chunked' Solution:
Allows streaming data in a series of un-buffered chunks:
1
Server omits 'Content-Length' and sets 'Transfer-Encoding: chunked'.
2
Each payload piece is sent as a hex byte size prefix, followed by '
' and chunk data.
3
A zero-length chunk ('0
') signals response completion!
Code Implementation Example
http
HTTP/1.1 200 OK
Transfer-Encoding: chunked
Content-Type: text/plain
7
Chunk 1
6
Chunk2
0
💡HTTP Transfer-Encoding: chunked raw payload transmission format.
🎯 Interview Key Takeaway
Transfer-Encoding: chunked streams dynamically generated API payloads immediately without buffering entire datasets in server memory.
#Chunked Encoding#Streaming#HTTP#Intermediate
Back to all REST APIs & Web Services Questions→