Aiohttp Web.response 添加 headers

Aiohhtp Web.response Adding headers

这是一个方法结束,它发送 body 中的匹配项和 headers 中的匹配项数。

.
. 
match_count = len(matches)
tot = {'total': match_count}
return web.json_response({"matches": fdata}, headers=tot)

在处理过程中出现以下服务器错误

File "/workspace/aio/server/aioenv/lib/python3.6/site-packages/aiohttp/http_writer.py", line 111, in write_headers
    buf = _serialize_headers(status_line, headers)
  File "aiohttp/_http_writer.pyx", line 138, in aiohttp._http_writer._serialize_headers
  File "aiohttp/_http_writer.pyx", line 110, in aiohttp._http_writer.to_str
TypeError: Cannot serialize non-str key 19

有些body请说明。 tot 必须是字典。正如文档所解释的那样,我如何将其转换为 str

Header 不能包含 int 值。您必须将其转换为字符串,如下所示:

tot = {'total': str(match_count)}