Could you please advise on the following?
On the localhost:8900
there is aiohttp server running
When I do a request like (using the python2 module requests) from python
requests.get("http://127.0.01:8900/api/bgp/show-route", data={'topo':"switzerland", 'pop':"zrh", 'prefix':"1.1.1.1/32"})
And there is a route defined in the aiohttp server
app.router.add_route("GET", "/api/bgp/show-route", api_bgp_show_route)
which is being handled like
def api_bgp_show_route(request): pass
The question is: how do I retrieve on server-side the data part of the request? meaning {'topo':"switzerland", 'pop':"zrh", 'prefix':"1.1.1.1/32"}
get is that requests fetches the whole body of the response at once and remembers it, but aiohttp doesn't. aiohttp lets you ignore the body, or read it in chunks, or read it after looking at the headers/status code. That's why you need to do a second await : aiohttp needs to do more I/O to get the response body.
By default the aiohttp. ClientSession object will hold a connector with a maximum of 100 connections, putting the rest in a queue. This is quite a big number, this means you must be connected to a hundred different servers (not pages!) concurrently before even having to consider if your task needs resource adjustment.
If you need to add HTTP headers to a request, pass them in a dict to the headers parameter. await session. post(url, data='Привет, Мир! ')
ahh the data
part is accessed like that
await request.json()
You can find this in official aiohttp docs
It depends on the format you want the data to be.
To get a string:
request.text()
To get bytes:
request.read()
To get a JSON dict (Attention, throws a json.decoder.JSONDecodeError if the data is malformatted!):
request.json()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With