Skip to content Skip to sidebar Skip to footer

How To Get The Request Body Bytes In Flask?

The request's content-type is application/json, but I want to get the request body bytes. Flask will auto convert the data to json. How do I get the request body?

Solution 1:

You can get the non-form-related data by calling request.get_data() You can get the parsed form data by accessing request.form and request.files.

However, the order in which you access these two will change what is returned from get_data. If you call it first, it will contain the full request body, including the raw form data. If you call it second, it will typically be empty, and form will be populated. If you want consistent behavior, call request.get_data(parse_form_data=True).

You can get the body parsed as JSON by using request.get_json(), but this does not happen automatically like your question suggests.

See the docs on dealing with request data for more information.

Solution 2:

Solution 3:

If you want the data as a string instead of bytes, use request.get_data(as_text=True). This will only work if the body is actually text, not binary, data.

Solution 4:

If the data is JSON, use request.get_json() to parse it.

Post a Comment for "How To Get The Request Body Bytes In Flask?"