Flask request.args vs request.form Flask request.args vs request.form flask flask

Flask request.args vs request.form


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.


Your json data in curl is wrong, so Flask does not parse data to form.

Send data like this: '{"name":"Joe"}'

curl -X POST -d '{"name":"Joe"}' http://example.com:8080/testpoint --header "Content-Type:application/json"