HomeAbout

StreamingHttpResponse

What is StreamingHttpResponse

With HttpResponse, entire file is typically loaded in memory before sent to the client.

This makes time-to-first-byte send to client longer with larger files.

With StreamingHttpResponse, response body is sent to the client in multiple pieces, or chunks.

Often used with yield as it returns a chunk of data at a time.

from django.http import StreamingHttpResponse def generate_large_file(): for i in range(1000000): yield f"Line {i}\n" response = StreamingHttpResponse(generate_large_file(), content_type="text/plain") return response

Response

If the streaming response worked, the client should get an HTTP 1.1 response with the Transfer-Encoding: chunked header, and instead of a single piece of content with a Content-Length.

The client should see each bytestring that your generator/iterator yielded, sent with the length of that chunk.

AboutContact