Streaming Chunked HTTP Responses in Go
Problem Statement:
When implementing a Go HTTP server that sends chunked responses, the server consistently delivers all chunks at the end of a specified duration instead of sending them incrementally. Additionally, Go automatically includes the Content-Length header with a value greater than zero, even when the content is not known in advance.
Solution:
To enable incremental sending of chunks and avoid setting the Content-Length header prematurely, follow these steps:
Example Code:
package main import ( "fmt" "log" "net/http" "time" ) func main() { http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { flusher, ok := w.(http.Flusher) if !ok { panic("expected http.ResponseWriter to be an http.Flusher") } w.Header().Set("X-Content-Type-Options", "nosniff") for i := 1; i <= 10; i++ { fmt.Fprintf(w, "Chunk #%d\n", i) flusher.Flush() time.Sleep(500 * time.Millisecond) } }) log.Print("Listening on localhost:8080") log.Fatal(http.ListenAndServe(":8080", nil)) }
Verification:
Testing the server using Telnet will show chunks being sent incrementally:
$ telnet localhost 8080 Trying ::1... Connected to localhost. Escape character is '^]'. GET / HTTP/1.1 HTTP/1.1 200 OK Date: Tue, 02 Jun 2015 18:16:38 GMT Content-Type: text/plain; charset=utf-8 Transfer-Encoding: chunked 9 Chunk #1 9 Chunk #2 ...
The above is the detailed content of How to Stream Chunked HTTP Responses Incrementally in Go?. For more information, please follow other related articles on the PHP Chinese website!