{"record":{"id":"1f4441aeba566099","repo":"valyala/fasthttp","slug":"fasthttp-missing-location-header-for-http-redirec","errorCode":null,"errorMessage":"fasthttp: missing location header for http redirect","messagePattern":"fasthttp: missing location header for http redirect","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"client.go","lineNumber":1223,"sourceCode":"\tdefer ReleaseRequest(req)\n\n\treq.Header.SetMethod(MethodPost)\n\treq.Header.SetContentTypeBytes(strPostArgsContentType)\n\tif postArgs != nil {\n\t\tif _, err := postArgs.WriteTo(req.BodyWriter()); err != nil {\n\t\t\treturn 0, nil, err\n\t\t}\n\t}\n\n\tstatusCode, body, err = doRequestFollowRedirectsBuffer(req, dst, url, c)\n\n\treturn statusCode, body, err\n}\n\nvar (\n\t// ErrMissingLocation is returned by clients when the Location header is missing on\n\t// an HTTP response with a redirect status code.\n\tErrMissingLocation = errors.New(\"fasthttp: missing location header for http redirect\")\n\t// ErrTooManyRedirects is returned by clients when the number of redirects followed\n\t// exceed the max count.\n\tErrTooManyRedirects = errors.New(\"fasthttp: too many redirects detected when doing the request\")\n\t// ErrRedirectBodyStream is returned by clients when a redirect that keeps the request\n\t// body is received for a request whose body is a stream. The hop that produced the\n\t// redirect consumed the stream, so the body cannot be sent again.\n\tErrRedirectBodyStream = errors.New(\"fasthttp: cannot follow a body-preserving redirect for a request with a body stream\")\n\n\t// ErrHostClientRedirectToDifferentScheme is returned when a HostClient follows a redirect to a different protocol.\n\tErrHostClientRedirectToDifferentScheme = errors.New(\"fasthttp: hostclient can't follow redirects to a different protocol,\" +\n\t\t\" please use client instead\")\n)\n\nconst defaultMaxRedirectsCount = 16\n\n// Only drain response streams that are known to be small. Reading an unknown\n// length stream can block forever (for example, on an event stream), while\n// closing a large stream avoids transferring a body the caller discarded.","sourceCodeStart":1205,"sourceCodeEnd":1241,"githubUrl":"https://github.com/valyala/fasthttp/blob/c96f600972c6f4a7a30d664257b340ebe9d60124/client.go#L1205-L1241","documentation":"ErrMissingLocation is returned by fasthttp clients when a response carries a redirect status code (3xx) but no Location header, so the client cannot determine where to redirect the request. Since redirect following is enabled, fasthttp refuses to proceed without a target.","triggerScenarios":"Using HostClient/Client with redirect following enabled and the server replies 301/302/303/307/308 without a Location header; verified by TestExportedErrorStrings coverage.","commonSituations":"Misconfigured or partially implemented endpoints returning 3xx manually without setting Location; reverse proxies stripping the header; hand-built responses in tests or gateways.","solutions":["Fix the server/proxy to always set the Location header on 3xx responses","Disable redirect following and handle 3xx responses manually in your code","Log response headers to identify which hop drops Location","If a middleware strips Location, re-add it before the response reaches the client"],"exampleFix":"// before: server side returning a bare redirect\nctx.SetStatusCode(fasthttp.StatusFound)\n// after: always set Location with the status\nctx.Response.Header.Set(\"Location\", \"/login\")\nctx.SetStatusCode(fasthttp.StatusFound)","handlingStrategy":"validation","validationCode":"loc := resp.Header.Peek(fasthttp.HeaderLocation)\nif isRedirect(resp.StatusCode()) && len(loc) == 0 {\n    // handle 3xx-without-Location yourself before redirect-following\n}","typeGuard":null,"tryCatchPattern":"if errors.Is(err, fasthttp.ErrMissingLocation) {\n    // inspect resp.StatusCode(); treat server as misbehaving\n}","preventionTips":["Test that server 3xx paths return Location","Audit reverse-proxy configs for header stripping","Handle 3xx manually when you do not trust the upstream","Add contract tests asserting Location on redirects"],"tags":["http","redirect","client"],"backgroundTag":"missing-location-header","analyzedSha":"c96f600972c6f4a7a30d664257b340ebe9d60124","analyzedAt":"2026-08-31T22:48:28.265Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T05:18:18.240Z"}