liveBook · Manning

HTTP response code

As explained in Go in Practice, Second Edition

An HTTP status code is a three-digit code in an HTTP response indicating the outcome of processing a request.

An HTTP response code communicates what happened while processing a request. Servers should return meaningful error codes and end failed handlers early; clients can inspect the numeric code or status text. section 8.3 section 11.2

Interpret response-code ranges before deciding how to handle a response:

  • Treat 20x responses as successful requests. section 11.2
  • Interpret 30x responses as redirects, 400-range responses as client errors, and 500-range responses as server errors. section 11.2.2
  • Use 401 or 403 responses to trigger authentication or reauthentication, and consider incremental backoff for 429 or 500 responses. section 11.2 section 11.2.2
  • Prefer Go constants such as http.StatusForbidden over unexplained numeric literals. section 8.3 section 11.2

Return and convert an HTTP error

A Go handler can return a plain-text 403 with http.Error(w, "An Error Occurred", http.StatusForbidden). A client can inspect res.StatusCode and Content-Type, then convert an unexpected non-2xx response into a custom error carrying the HTTP code, application code, and message.

How Go in Practice, Second Edition covers it

How deep it goes
Descends from status ranges to Go constants, response inspection, MIME checks, and custom errors, but leaves retry implementation incomplete.
Examples
Uses Go handlers, curl requests, status inspection, range classification, and custom JSON error conversion.
What it assumes
Assumes basic Go HTTP server and client usage while demonstrating status constants, response fields, and the Go error interface.
What you can do after
Diagnose/debug HTTP failures by classifying response codes, checking content types, authenticating, retrying selectively, and returning structured Go errors.
How it connects
Connects HTTP response codes to HTTP errors, authentication, MIME types, middleware, and retry strategies.

Also explained in

Compare how 11 books explain HTTP response code →

Related concepts

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