I want to be able to get the data sent to my Flask app. I've tried accessing request.data
but it is an empty string. How do you access request data?
@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.
if you want the raw post body regardless of the content type, you should use
request.get_data()
, becauserequest.form
is converted towerkzeug.ImmutableMultiDict
format.It is simply as follows
For URL Query parameter, use request.args
For Form input, use request.form
For data type application/json, use request.data
I give a full example of application/json:
use Postman for post request:
use curl command:
P.S. For URL Query parameter example, you can see my answer in Multiple parameters in in Flask approute
Now, data is the request body
Flask has another shortcut for JSON:
Header:
Using
request.form
.Instead of getting a single form data (
request.form["field_name"]
), you can obtain all posted data, by parsing theImmutableDict
provided byrequest.form
object, like this:Flask (Route)
Shell
For more details, this Gist.