{"record":{"id":"5a91df7f6d677713","repo":"valyala/fasthttp","slug":"missing-required-host-header-in-request","errorCode":null,"errorMessage":"missing required host header in request","messagePattern":"missing required host header in request","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"http.go","lineNumber":1712,"sourceCode":"\t}\n\n\tswitch {\n\tcase contentLength >= 0:\n\t\tbodyBuf.B, err = readBody(r, contentLength, maxBodySize, bodyBuf.B)\n\tcase contentLength == -1:\n\t\tbodyBuf.B, err = readBodyChunked(r, maxBodySize, bodyBuf.B)\n\tdefault:\n\t\tbodyBuf.B, err = readBodyIdentity(r, maxBodySize, bodyBuf.B)\n\t\tresp.Header.SetContentLength(len(bodyBuf.B))\n\t}\n\treturn err\n}\n\nfunc (resp *Response) mustSkipBody() bool {\n\treturn resp.SkipBody || resp.Header.mustSkipContentLength()\n}\n\nvar errRequestHostRequired = errors.New(\"missing required host header in request\")\n\n// WriteTo writes request to w. It implements io.WriterTo.\nfunc (req *Request) WriteTo(w io.Writer) (int64, error) {\n\treturn writeBufio(req, w)\n}\n\n// WriteTo writes response to w. It implements io.WriterTo.\nfunc (resp *Response) WriteTo(w io.Writer) (int64, error) {\n\treturn writeBufio(resp, w)\n}\n\nfunc writeBufio(hw httpWriter, w io.Writer) (int64, error) {\n\tsw := acquireStatsWriter(w)\n\tbw := acquireBufioWriter(sw)\n\terrw := hw.Write(bw)\n\terrf := bw.Flush()\n\treleaseBufioWriter(bw)\n\tn := sw.bytesWritten","sourceCodeStart":1694,"sourceCodeEnd":1730,"githubUrl":"https://github.com/valyala/fasthttp/blob/c96f600972c6f4a7a30d664257b340ebe9d60124/http.go#L1694-L1730","documentation":"fasthttp's Request.WriteTo / write path refuses to serialize an HTTP request that has no Host header set. HTTP/1.1 requires a Host header, so writing a request without one is treated as a protocol violation rather than silently sending a broken request.","triggerScenarios":"Calling Request.WriteTo (or client Do methods that serialize the request) on a Request whose Header.Host (and RequestURI authority) is empty; clearing the Host header manually before sending.","commonSituations":"Building a Request by hand with fasthttp.AcquireRequest and forgetting req.Header.SetMethod/SetHost or SetRequestURI; copying headers from another request and dropping Host; using a proxy client that expects the URI to carry the authority but passing an origin-form URI.","solutions":["Set the host before sending: req.Header.SetHost(\"example.com\") or pass a full URL to req.SetRequestURI(\"http://example.com/path\") so fasthttp derives the Host header.","If building requests manually, use fasthttp.AcquireRequest and always set Host via SetHost or SetRequestURI.","If proxying, ensure the forwarded request retains the original Host header instead of clearing it."],"exampleFix":"// before\nreq := fasthttp.AcquireRequest()\nreq.SetRequestURI(\"/api/v1/users\")\nerr := client.Do(req, resp) // missing required host header in request\n// after\nreq := fasthttp.AcquireRequest()\nreq.SetRequestURI(\"http://example.com/api/v1/users\")\n// or: req.Header.SetHost(\"example.com\")\nerr := client.Do(req, resp)","handlingStrategy":"validation","validationCode":"func requireHost(req *fasthttp.Request) error {\n    if len(req.Header.Host()) == 0 && !bytes.Contains(req.URI().FullURI(), []byte(\"//\")) {\n        return errors.New(\"request has no Host; call SetHost or SetRequestURI with absolute URL\")\n    }\n    return nil\n}","typeGuard":null,"tryCatchPattern":"err := client.Do(req, resp)\nif err != nil && strings.Contains(err.Error(), \"missing required host header\") {\n    req.Header.SetHost(defaultHost)\n    err = client.Do(req, resp)\n}","preventionTips":["Always set the host immediately after AcquireRequest.","Prefer SetRequestURI with an absolute URL so Host is derived.","Lint/proxy-test outbound requests with curl -v to verify Host presence."],"tags":["http","host-header","request-serialization"],"backgroundTag":"missing-host-header","analyzedSha":"c96f600972c6f4a7a30d664257b340ebe9d60124","analyzedAt":"2026-08-31T22:48:28.265Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T05:18:18.240Z"}