问题描述
在使用 aiohttp 时,你可能会遇到以下特有的错误:
aiohttp.client_exceptions.ClientResponseError: 400, message='Got more than 8190 bytes (11984) when reading Header value is too long.
这个错误是由于请求头的大小超过了 aiohttp 的默认限制 8190 字节。这是 aiohttp 独有的错误,其他 HTTP 客户端可能不会遇到类似问题。
初步解决方案
- 修改 HeadersParser
我们可以尝试通过猴子补丁(monkey patching)修改 HeadersParser 来改变 aiohttp 的默认行为:
from aiohttp.http_parser import HeadersParser
def monkey_init(
self,
max_line_size: int = 8190,
max_headers: int = 32768,
max_field_size: int = 8190,
) -> None:
self.max_line_size = max_line_size
self.max_headers = max_headers
self.max_field_size = 2 << 31
HeadersParser.__init__ &#