question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Aiohttp: Parser.parse doesn't work after request body was read.

See original GitHub issue

When 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:closed
  • Created 6 years ago
  • Comments:14 (11 by maintainers)

github_iconTop GitHub Comments

2reactions
sloriacommented, Feb 8, 2018

@lafrech. I agree. Ok, I’ll release those when I’m off the clock.

1reaction
sloriacommented, Jun 23, 2017

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found