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.

Prevents access to request raw data in Flask

See original GitHub issue

This all seems to be a bit of a mess, but if you want to access the raw request data in Flask regardless of the content type (I can’t control the client), you need to call

 request.get_data()

and you need to do this before accessing request.data or request.form, because both of these will call get_data() in such a way that the stream will be exhausted, parsed into the form dict, and the raw data is never cached.

This doesn’t seem documented except in the comments: https://github.com/mitsuhiko/werkzeug/blob/master/werkzeug/wrappers.py#L440

Should the sentry extension call request.get_data() first to make sure the raw data is cached before using request.form?

Issue Analytics

  • State:open
  • Created 9 years ago
  • Reactions:6
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
groverifficcommented, Mar 4, 2016

I’ve run into this issue also recently.

It makes sense that Flask doesn’t want to cache the unparsed body. This could be really large in the case of file uploads. Triggering the caching is probably not best for all scenarios.

This is working for me right now

# Workaround to allow unparsed request body to be be read from cache
# This is required to validate a signature on webhooks
# This MUST go before Sentry integration as sentry triggers form parsing
@app.before_request
def enable_form_raw_cache():
  if request.path.startswith('/redacted'):
    if request.content_length > 1024 * 1024:  # 1mb
      abort(413)  # Payload too large
    request.get_data(parse_form_data=False, cache=True)

if config.SENTRY_DSN:
  sentry = Sentry(app, dsn=config.SENTRY_DSN, logging=True, level=logging.ERROR)

There might still be a better, more general solution. I’m not sure if the code that captures request.form could just be moved into an after_request and still have the same results?

0reactions
mitsuhikocommented, Sep 25, 2018

For what it’s worth the new sentry-python SDK makes this configurable and generally is less invasive for accessing this information.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How To Process Incoming Request Data in Flask - DigitalOcean
The request object holds all incoming data from the request, which includes the mimetype, referrer, IP address, raw data, HTTP method, and ...
Read more >
Get raw POST body in Python Flask regardless of Content ...
Use request.get_data() to get the raw data, regardless of content type. The data is cached and you can subsequently access request.data ...
Read more >
API — Flask Documentation (2.2.x)
A data structure of functions to call at the end of each request, in the format {scope: [functions]} . The scope key is...
Read more >
How to Process Incoming Request Data in Flask - YouTube
In this video I talk about how to process incoming data in Flask. I cover three cases: url parameters (query arguments), form data, ......
Read more >
Flask by Example – Text Processing with Requests ...
by Real Python 35 Comments data-science flask web-dev ... Free Bonus: Click here to get access to a free Flask + Python video...
Read more >

github_iconTop Related Medium Post

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