valyala/fasthttp · error

missing required host header in request

Error message

missing required host header in request

What it means

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.

Source

Thrown at http.go:1712

	}

	switch {
	case contentLength >= 0:
		bodyBuf.B, err = readBody(r, contentLength, maxBodySize, bodyBuf.B)
	case contentLength == -1:
		bodyBuf.B, err = readBodyChunked(r, maxBodySize, bodyBuf.B)
	default:
		bodyBuf.B, err = readBodyIdentity(r, maxBodySize, bodyBuf.B)
		resp.Header.SetContentLength(len(bodyBuf.B))
	}
	return err
}

func (resp *Response) mustSkipBody() bool {
	return resp.SkipBody || resp.Header.mustSkipContentLength()
}

var errRequestHostRequired = errors.New("missing required host header in request")

// WriteTo writes request to w. It implements io.WriterTo.
func (req *Request) WriteTo(w io.Writer) (int64, error) {
	return writeBufio(req, w)
}

// WriteTo writes response to w. It implements io.WriterTo.
func (resp *Response) WriteTo(w io.Writer) (int64, error) {
	return writeBufio(resp, w)
}

func writeBufio(hw httpWriter, w io.Writer) (int64, error) {
	sw := acquireStatsWriter(w)
	bw := acquireBufioWriter(sw)
	errw := hw.Write(bw)
	errf := bw.Flush()
	releaseBufioWriter(bw)
	n := sw.bytesWritten

View on GitHub (pinned to c96f600972)

Solutions

  1. 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.
  2. If building requests manually, use fasthttp.AcquireRequest and always set Host via SetHost or SetRequestURI.
  3. If proxying, ensure the forwarded request retains the original Host header instead of clearing it.

Example fix

// before
req := fasthttp.AcquireRequest()
req.SetRequestURI("/api/v1/users")
err := client.Do(req, resp) // missing required host header in request
// after
req := fasthttp.AcquireRequest()
req.SetRequestURI("http://example.com/api/v1/users")
// or: req.Header.SetHost("example.com")
err := client.Do(req, resp)
Defensive patterns

Strategy: validation

Validate before calling

func requireHost(req *fasthttp.Request) error {
    if len(req.Header.Host()) == 0 && !bytes.Contains(req.URI().FullURI(), []byte("//")) {
        return errors.New("request has no Host; call SetHost or SetRequestURI with absolute URL")
    }
    return nil
}

Try / catch

err := client.Do(req, resp)
if err != nil && strings.Contains(err.Error(), "missing required host header") {
    req.Header.SetHost(defaultHost)
    err = client.Do(req, resp)
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of valyala/fasthttp@c96f600972 (2026-08-31). Data as JSON: /api/errors/5a91df7f6d677713. Report an issue: GitHub.