liveBook · Manning

Http.ResponseWriter

As explained in Go by Example

http.ResponseWriter is the Go HTTP interface through which a handler constructs an HTTP response and accesses connection-related response behavior.

http.ResponseWriter is the response side of a Go HTTP handler. A server passes it with *http.Request to ServeHTTP, and the handler uses it to construct the client response. Header sets response headers, WriteHeader sets the status code, and Write sends body bytes. Set headers and status before writing the body; if no status is set first, writing produces the default 200 OK response. section 8.3

A minimal health response

A health handler can pass its ResponseWriter to fmt.Fprintln because ResponseWriter is also a Writer. Writing "OK" is enough to return a successful 200 response without an explicit WriteHeader call, making a small endpoint useful for availability checks. section 8.3.1

ResponseWriter also makes handlers easy to test: supply an httptest.ResponseRecorder and a request, call the handler directly, then inspect the recorded status and body. The recorder implements ResponseWriter, so production handler code needs no special testing branch. section 8.7 section 8.7.1

The base interface stays small, while concrete response writers may offer optional behavior. Type-assert http.Flusher before flushing buffered data; related optional interfaces include http.Pusher and http.Hijacker. Middleware that wraps a ResponseWriter can accidentally hide such capabilities, so an Unwrap method can expose the original writer when that behavior is required. section 9.4.1 section 9.8.2

Figure 8.6 The http.Server routes incoming HTTP requests to its http.Handler by calling the Handler’s ServeHTTP method, passing an http.ResponseWriter and http.Request. Then the Handler can use the ResponseWriter to respond to clients via the Server.
A diagram of a server

Description automatically generated

Follow the request path from http.Server to a handler and back through its ResponseWriter. section 8.3

Listing 9.27 Unwrapping ResponseWriters (link/kit/hio/request.go)
// MaxBytesReader is like [http.MaxBytesReader], but unwraps the
// original [http.ResponseWriter] if it's wrapped.
func MaxBytesReader(
    w http.ResponseWriter, rc io.ReadCloser, max int64,
) io.ReadCloser {
  type unwrapper interface { #1
    Unwrap() http.ResponseWriter  #1
  }  #1
  for { #2
    v, ok := w.(unwrapper) #3
    if !ok {
      break #4
    }
    w = v.Unwrap() #5
  }
  return http.MaxBytesReader(w, rc, max)
}

Inspect a helper that repeatedly unwraps middleware layers before passing the original writer to MaxBytesReader. section 9.8.2

How Go by Example covers it

How deep it goes
Defines core response methods, ordering, default status behavior, testing, optional interfaces, and wrapper unwrapping.
Examples
Uses an OK health endpoint, a ResponseRecorder test, conditional flushing, and oversized-request connection closure.
What it assumes
Assumes familiarity with Go interfaces, handlers, and type assertions while explaining ResponseWriter's response methods.
What you can do after
Apply it via the net/http package to send, test, flush, and unwrap handler responses.
How it connects
Connects HTTP handlers, http.Server, *http.Request, httptest.ResponseRecorder, type assertions, middleware, and http.MaxBytesReader.

Also explained in

Compare how 2 books explain http.ResponseWriter →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Go by Example. The section links point to the parts of the book it draws on.