aio-libs/aiohttp · error · BadHttpMessage
Missing 'Host' header in request.
Error message
Missing 'Host' header in request.
What it means
Raised by HttpRequestParser.parse_message (aiohttp/http_parser.py:717) when an HTTP/1.1 request has no Host header. RFC 9110 section 7.2 makes Host mandatory for HTTP/1.1. HTTP/1.0 requests are not required to send Host (though encouraged), so this only fires for version 1.1.
Source
Thrown at aiohttp/http_parser.py:717
if not url.absolute:
# authority-form is only allowed with CONNECT
# https://www.rfc-editor.org/info/rfc9112/#section-3.2.3-1
raise InvalidURLError(
path.encode(errors="surrogateescape").decode("latin1")
)
# read headers
(
headers,
raw_headers,
close,
compression,
upgrade,
chunked,
) = self.parse_headers(lines[1:])
if version_o == HttpVersion11 and hdrs.HOST not in headers:
raise BadHttpMessage("Missing 'Host' header in request.")
if close is None: # then the headers weren't set in the request
if version_o <= HttpVersion10: # HTTP 1.0 must asks to not close
close = True
else: # HTTP 1.1 must ask to close.
close = False
return RawRequestMessage(
method,
path,
version_o,
headers,
raw_headers,
close,
compression,
upgrade,
chunked,
url,View on GitHub (pinned to c0ef574e29)
Solutions
- Include a 'Host' header in every HTTP/1.1 request.
- If you intend HTTP/1.0 semantics, send 'HTTP/1.0' in the request line (Host not required).
- Use a real HTTP client (aiohttp.ClientSession) which sets Host automatically from the URL.
Example fix
# before - HTTP/1.1 with no Host sock.send(b'GET / HTTP/1.1\r\n\r\n') # after - include Host sock.send(b'GET / HTTP/1.1\r\nHost: example.com\r\n\r\n') # or downgrade to HTTP/1.0 if Host is unavailable sock.send(b'GET / HTTP/1.0\r\n\r\n')
Defensive patterns
Strategy: validation
Validate before calling
def host_ok(version: str, headers) -> bool:
if version == 'HTTP/1.1':
return any(k.lower() == 'host' for k in headers)
return True # HTTP/1.0 does not require Host
assert host_ok(version_token, outgoing_headers) Prevention
- Always send a Host header in HTTP/1.1 requests
- Use an HTTP client that sets Host automatically
- Downgrade to HTTP/1.0 only if you cannot send Host
When it happens
Trigger: An HTTP/1.1 request line ('... HTTP/1.1') with no 'Host' header in the header block. Common with raw socket/netcat clients, telnet, or HTTP/1.0 clients that were bumped to 1.1 without adding Host.
Common situations: netcat/telnet health checks, embedded clients that build requests by hand, load-balancer probes that strip Host, or proxy misconfiguration that drops the header.
Related errors
- Duplicate '{name}' header found.
- Invalid Content-Length header: {content_length_hdr!r}
- Got more than {limit} bytes when reading: {line!r}.
- Bad line ending, expected CRLF
- Transfer-Encoding can't be present with Content-Length
AI-assisted analysis of aio-libs/aiohttp@c0ef574e29 (2026-08-04).
Data as JSON: /data/errors/b8e6e4c1d436f40f.json.
Report an issue: GitHub.