Home > Backend Development > Golang > How to Stream Chunked HTTP Responses Incrementally in Go?

How to Stream Chunked HTTP Responses Incrementally in Go?

Susan Sarandon
Release: 2024-12-10 01:26:12
Original
204 people have browsed it

How to Stream Chunked HTTP Responses Incrementally in Go?

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:

  1. Eliminate Transfer-Encoding Header: Go automatically handles the Transfer-Encoding header when the writer sends chunked responses. Therefore, there is no need to explicitly set it.
  2. Embrace Flush: To flush each chunk immediately after writing it to the response stream, use http.ResponseWriter.Flush(). This will trigger chunked encoding and send the chunk without waiting for the response to complete.

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))
}
Copy after login

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

...
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template