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?
Use request. get_data() to get the raw data, regardless of content type. The data is cached and you can subsequently access request.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With