Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flask - How do I read the raw body in a POST request when the content type is "application/x-www-form-urlencoded"

Turns out that Flask sets request.data to an empty string if the content type of the request is application/x-www-form-urlencoded. Since I'm using a JSON body request, I just want to parse the json or force Flask to parse it and return request.json.

This is needed because changing the AJAX content type forces an HTTP OPTION request, which complicates the back-end.

How do I make Flask return the raw data in the request object?

like image 877
Ron Reiter Avatar asked Jul 14 '13 15:07

Ron Reiter


People also ask

How do I get raw data from Flask?

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

How do I read application X www form Urlencoded in Python?

If you want to decode or parse multiple query strings of type application/x-www-form-urlencoded (e.g 'name=Rajeev+Singh&phone=%2B919999999999' ), then you can use parse_qs or parse_qsl functions provided by urllib.

How do I read a post request in Flask?

If you post JSON with content type application/json , use request. get_json() to get it in Flask. If the content type is not correct, None is returned. If the data is not JSON, an error is raised.


1 Answers

You can get the post data via request.form.keys()[0] if content type is application/x-www-form-urlencoded.

request.form is a multidict, whose keys contain the parsed post data.

like image 137
iMom0 Avatar answered Oct 22 '22 01:10

iMom0