ddinchev ddinchev - 1 year ago 71
Python Question

How to get the data recieved in a Flask request

I want to be able to get the data received by a request to my Flask app. However, accessing

always gives the empty string. What do I use to access the data for a request?

@app.route('/', methods=['GET', 'POST'])
def parse_request():
data = request.data # data is empty
# need posted data here

The answer to this question led me to ask Get raw POST body in Python Flask regardless of Content-Type header next, which is about getting the raw data rather than the parsed data.

Answer Source

The docs describe the attributes available on the request. In most common cases request.data will be empty because it's used as a fallback:

request.data Contains the incoming request data as string in case it came with a mimetype Flask does not handle.

  • request.args: the key/value pairs in the URL query string
  • request.form: the key/value pairs in the body, as sent by a HTML POST form
  • request.files: the files in the body, which Flask keeps separate from form
  • request.values: combined args and form, preferring args if keys overlap
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download