http.ErrNoLocation
GoERRORCommonHTTP

http: no Location header in response

Quick Answer

Always check the error from resp.Location() before following the redirect URL.

What this means

Returned by Response.Location when the server did not include a Location header, typically expected after a redirect response.

Why it happens
  1. 1Server returned a 3xx status without a Location header
  2. 2Manually calling Location on a non-redirect response

Fix

Check before use

Check before use
loc, err := resp.Location()
if errors.Is(err, http.ErrNoLocation) {
    log.Println("no redirect location")
    return
}

Why this works

Guarding prevents a nil URL dereference when the header is absent.

Code examples
Detectgo
loc, err := resp.Location()
if errors.Is(err, http.ErrNoLocation) {
    fmt.Println("missing Location")
}
Manual redirectgo
http.Redirect(w, r,
    "/new-path", http.StatusMovedPermanently)
Follow redirectgo
if loc, err := resp.Location(); err == nil {
    fmt.Println("redirect to:", loc)
}
Sources
Official documentation ↗

Go standard library

Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev

← All Go errors