http.ResponseWriter
The Go interface a handler uses to send its response: set headers, write the status code with WriteHeader, then write the body.
In more detail
The order matters: headers first, then status, then body. If you write the body without calling WriteHeader, the status is 200. It is an io.Writer, so fmt.Fprintln and json encoders can write to it directly.
Example
w.WriteHeader(http.StatusServiceUnavailable)Related terms
- http.Handler — In Go, a function or type that receives an HTTP request and writes the response, with the signature func(w http.ResponseWriter, r *http.Request).
- HTTP status code — The three-digit number at the start of a response, such as 200 or 404, that says how the server handled the request.
- HTTP header — A named line of metadata in an HTTP message, such as Content-Type: application/json, that describes the body, the caller or how to handle the exchange.
- io.Writer — The Go interface for anything you can write bytes to, such as a file, standard output, a buffer or an HTTP response.
Taught in
- Serving HTTP with net/http Server — Concurrent Systems Programming with Go