StatusText Maps Codes To Reason Phrases
http.StatusText turns numeric status codes into canonical reason phrases and returns an empty string for unregistered codes like 599.
Code
package main
import (
"fmt"
"net/http"
)
func main() {
// StatusText maps status codes back to canonical reason phrases
for _, code := range []int{
http.StatusContinue,
http.StatusOK,
http.StatusMovedPermanently,
http.StatusBadRequest,
http.StatusUnauthorized,
http.StatusNotFound,
http.StatusInternalServerError,
http.StatusBadGateway,
} {
fmt.Println(code, "->", http.StatusText(code))
}
// unregistered codes yield the empty string, no invented text
fmt.Println("unknown:", http.StatusText(599))
}What the demo maps
The loop walks eight well-known codes, 100 through 502, and prints each beside http.StatusText of it, producing the canonical reason phrases Continue, OK, Moved Permanently, Bad Request, Unauthorized, Not Found, Internal Server Error and Bad Gateway straight from net/http.
The last line asks for 599, which no standard registers. StatusText returns the empty string rather than inventing text, so log rendering can treat an empty phrase as an unknown code instead of trusting a fabricated label.
Points to remember
- The mapping is the same table servers use to write status lines.
- Custom codes stay printable; the demo logs 599 against an empty phrase.
Interactive Tools for This Topic
cURL to Python & Go ConverterTranslate curl CLI commands with headers, auth, JSON bodies, and timeouts into ready-to-run Python requests and Go net/http client code snippets.HTTP Status Code ExplorerBrowse and explore HTTP status codes with their meanings, categories, and common use cases. Search by code or description to understand 1xx through 5xx responses.Accept Header ParserParse an Accept header into media ranges with their quality values and specificity, sorted by preference for content negotiation debugging.Basic & Bearer Auth Header ToolEncode and decode HTTP Authorization Basic and Bearer headers locally with UTF-8 Base64 validation and automatic JWT claim and expiry inspection.
FAQ
- What does StatusText return for an unregistered code?
- An empty string; 599 is not in the standard table, so nothing is invented and the demo prints the empty result explicitly.
Report a problem with this exampleOpens a pre-filled issue on GitHub.