They read three different parts of a request. request.args is the URL query string, request.form is a form-encoded body, and request.get_json() parses a JSON body.
request.args |
request.form |
request.get_json() |
|
|---|---|---|---|
| Source | Query string after ? |
Request body | Request body |
| Content type | Any | application/x-www-form-urlencoded or multipart/form-data |
application/json |
| Returns | MultiDict of strings |
MultiDict of strings |
Parsed dict or list |
| When the data is absent | get() returns None |
get() returns None |
415 error, or None with silent=True |
# URL: /api?page=1
# Body: {"name": "John"}
@app.route('/api', methods=['POST'])
def api():
page = request.args.get('page') # "1"
name = request.get_json()['name'] # "John"
username = request.form.get('username') # None, because the body is JSON
A request has only one body, so form and get_json() never both get data from the same request. That is why username is None above. When either source is fine, request.values combines args and form. Use args for filters and paging, form for HTML form posts, and get_json() for API clients.
This answer doesn't lend itself to a diagram - it reads best . No credits were charged.
Why there's no diagram: “”
The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓
The diagram below the answer is the concept . Jump to it ↓