aiohttp:如何从 requests.get 检索 aiohttp 服务器中的数据(正文)
aiohttp: how-to retrieve the data (body) in aiohttp server from requests.get
能否请您提供以下建议?
在 localhost:8900
上有 aiohttp 服务器 运行
当我从 python
发出类似(使用 python2 模块请求)的请求时requests.get("http://127.0.01:8900/api/bgp/show-route",
data={'topo':"switzerland",
'pop':"zrh",
'prefix':"1.1.1.1/32"})
并且在aiohttp服务器中定义了一条路由
app.router.add_route("GET", "/api/bgp/show-route", api_bgp_show_route)
处理方式与
相同def api_bgp_show_route(request):
pass
问题是:如何在服务器端检索请求的数据部分?意思是 {'topo':"switzerland", 'pop':"zrh", 'prefix':"1.1.1.1/32"}
啊 data
部分是这样访问的
await request.json()
你可以在官方找到这个aiohttp docs
您可以使用
访问POST请求正文数据if request.body_exists:
print(await request.read())
这取决于您希望数据的格式。
获取字符串:
request.text()
获取字节:
request.read()
要获取 JSON 字典(注意,如果数据格式错误,将抛出 json.decoder.JSONDecodeError!):
request.json()