My understanding is that request.args
in Flask contains the URL encoded parameters from a GET
request while request.form
contains POST
data. What I'm having a hard time grasping is why when sending a POST
request, trying to access the data with request.form
returns a 400
error but when I try to access it with request.args
it seems to work fine.
I have tried sending the request with both Postman
and curl
and the results are identical.
curl -X POST -d {"name":"Joe"} http://127.0.0.1:8080/testpoint --header "Content-Type:application/json"
Code:
@app.route('/testpoint', methods = ['POST']) def testpoint(): name = request.args.get('name', '') return jsonify(name = name)
request.args is a MultiDict with the parsed contents of the query string. From the documentation of get method: get(key, default=None, type=None) Return the default value if the requested data doesn't exist.
In the client-server architecture, the request object contains all the data that is sent from the client to the server. As we have already discussed in the tutorial, we can retrieve the data at the server side using the HTTP methods.
To access the incoming data in Flask, you have to use the request object. The request object holds all incoming data from the request, which includes the mimetype, referrer, IP address, raw data, HTTP method, and headers, among other things.
Request. get_json (force=False, silent=False, cache=True)[source] Parses the incoming JSON request data and returns it. By default this function will return None if the mimetype is not application/json but this can be overridden by the force parameter.
You are POST-ing JSON, neither request.args
nor request.form
will work.
request.form
works only if you POST data with the right content types; form data is either POSTed with the application/x-www-form-urlencoded
or multipart/form-data
encodings.
When you use application/json
, you are no longer POSTing form data. Use request.get_json()
to access JSON POST data instead:
@app.route('/testpoint', methods = ['POST']) def testpoint(): name = request.get_json().get('name', '') return jsonify(name = name)
As you state, request.args
only ever contains values included in the request query string, the optional part of a URL after the ?
question mark. Since it’s part of the URL, it is independent from the POST request body.
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