Aiohttp:如何从request .get中检索Aiohttp服务器中的数据(主体)



您能告诉我以下几点吗?

localhost:8900上有aiohttp服务器在运行

当我做一个请求(使用python2模块请求)从python

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文档

中找到。

这取决于你想要的数据格式。

获取字符串:

request.text()

获取字节:

request.read()

获取JSON字典(注意,如果数据格式错误,将抛出JSON .decoder. jsondecodeerror):

request.json()

您可以使用

访问POST请求体数据
if request.body_exists:
        print(await request.read())

最新更新