Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the request body bytes in Flask?

Tags:

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?

like image 411
Dozer Avatar asked Apr 19 '16 07:04

Dozer


1 Answers

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.

like image 173
davidism Avatar answered Oct 11 '22 14:10

davidism