Aiohttp: Parser.parse doesn't work after request body was read.
See original GitHub issueWhen a content of a request is read before parser.parse
is called (with location=('json',)
), parser returns that JSON fields are missing in the request.
Example code:
async def handler(request):
data = await request.json()
args = await parser.parse(handler_args, req=request, locations=('json',))
return web.Response()
The issue is the following line of code https://github.com/sloria/webargs/blob/dev/webargs/aiohttpparser.py#L92 specifically req.has_body
. This attribute has an unfortunate name and it’s not constant over a lifetime of a request. It checks if there are more bytes to read. Once the body is read by await request.json()
there are no more bytes to read and value of req.has_body
is False
.
Issue Analytics
- State:
- Created 6 years ago
- Comments:14 (11 by maintainers)
Top Results From Across the Web
Aiohttp: Parser.parse doesn't work after request body was read.
When a content of a request is read before parser.parse is called (with location=('json',)), parser returns that JSON fields are missing in ...
Read more >Advanced Client Usage — aiohttp 3.8.3 documentation
Create the session first, use the instance for performing HTTP requests and initiating WebSocket connections. The session contains a cookie storage and ...
Read more >how-to retrieve the data (body) in aiohttp server from requests ...
To get bytes: request.read(). To get a JSON dict (Attention, throws a json.decoder.JSONDecodeError if the data is malformatted!): request.json().
Read more >Streams — Python 3.11.1 documentation
Source code: Lib/asyncio/streams.py Streams are high-level async/await-ready primitives to work with network connections. Streams allow sending and ...
Read more >Essential Guide To Asynchronous Web Scraping In Python
Practice downloading multiple webpages using Aiohttp + Asyncio and parsing HTML content per URL with BeautifulSoup. The following python installations are for a ......
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@lafrech. I agree. Ok, I’ll release those when I’m off the clock.
Thanks! Good call on reporting that issue on aiohttp; looks like the maintainers are open to changing the behavior or adding a separate method.
Since webargs will need to support the current releases of aiohttp, it will need to be fixed in webargs, so a PR would be greatly appreciated.