aiohttp web.response 正文为 json
aiohttp web.response body as json
我在 aiohttp
和 python-3.6 上有 HTTP 服务器。我怎样才能 return web.Response()
通过 JSON (来自 dict
)?
async def api_server(request):
res = {"q": "qqq", "a": "aaa"}
return web.Response(res) # <-- as JSON
您可以使用 web.json_response
:
async def api_server(request):
res = {"q": "qqq", "a": "aaa"}
return web.<b>json_response</b>(res)
此外 json_response
还有其他参数,例如:
json_response(data, text=None, body=None, status=200, reason=None,
headers=None, content_type='application/json', <b>dumps</b>=json.dumps)
大部分参数与通用 web.Response(..)
相同,但 dumps
更有趣:它是对将数据转换成其 JSON 等价物的方法的引用.默认情况下它使用 json.dumps
。但是,如果您计划向客户端写入复杂的对象,您也许应该改变它。不过现在还好。
我在 aiohttp
和 python-3.6 上有 HTTP 服务器。我怎样才能 return web.Response()
通过 JSON (来自 dict
)?
async def api_server(request):
res = {"q": "qqq", "a": "aaa"}
return web.Response(res) # <-- as JSON
您可以使用 web.json_response
:
async def api_server(request):
res = {"q": "qqq", "a": "aaa"}
return web.<b>json_response</b>(res)
此外 json_response
还有其他参数,例如:
json_response(data, text=None, body=None, status=200, reason=None, headers=None, content_type='application/json', <b>dumps</b>=json.dumps)
大部分参数与通用 web.Response(..)
相同,但 dumps
更有趣:它是对将数据转换成其 JSON 等价物的方法的引用.默认情况下它使用 json.dumps
。但是,如果您计划向客户端写入复杂的对象,您也许应该改变它。不过现在还好。