Get raw POST body in Python Flask regardless of Content-Type header


Previously, I asked How to get data received in Flask request because was empty. The answer explained that is the raw post body, but will be empty if form data is parsed. How can I get the raw post body unconditionally?

@app.route('/', methods=['POST'])
def parse_request():
    data =  # empty in some cases
    # always need raw data here, not parsed form data

This question is tagged with python flask werkzeug

~ Asked on 2012-06-12 15:37:10

The Best Answer is


Use request.get_data() to get the raw data, regardless of content type. The data is cached and you can subsequently access, request.json, request.form at will.

If you access first, it will call get_data with an argument to parse form data first. If the request has a form content type (multipart/form-data, application/x-www-form-urlencoded, or application/x-url-encoded) then the raw data will be consumed. and request.json will appear empty in this case.

~ Answered on 2014-05-27 21:06:57

38 is the stream of raw data passed to the application by the WSGI server. No parsing is done when reading it, although you usually want request.get_data() instead.

data =

The stream will be empty if it was previously read by or another attribute.

~ Answered on 2012-06-12 16:25:25

Most Viewed Questions: