How to Get Request Data in Flask: Simple Guide
In Flask, you get request data using the
request object from flask. Use request.args for URL query parameters, request.form for form data, and request.json for JSON payloads in POST requests.Syntax
The request object in Flask provides different attributes to access data sent by the client:
request.args: Access URL query parameters as a dictionary.request.form: Access form data submitted via POST.request.json: Access JSON data sent in the request body.
Import request from flask to use these.
python
from flask import Flask, request app = Flask(__name__) @app.route('/example', methods=['GET', 'POST']) def example(): # Get query parameter 'name' name = request.args.get('name') # Get form field 'email' email = request.form.get('email') # Get JSON data data = request.json return f"Name: {name}, Email: {email}, JSON: {data}"
Example
This example shows a Flask app that reads data from URL query parameters, form data, and JSON body. It returns all received data as a string.
python
from flask import Flask, request, jsonify app = Flask(__name__) @app.route('/data', methods=['GET', 'POST']) def data(): # Get query parameter 'user' user = request.args.get('user', 'No user') # Get form data 'email' email = request.form.get('email', 'No email') # Get JSON data json_data = request.json or {} return jsonify({ 'user_query': user, 'email_form': email, 'json_body': json_data }) if __name__ == '__main__': app.run(debug=True)
Output
Running the app and sending requests:
GET /data?user=alice
Response: {"user_query":"alice","email_form":"No email","json_body":{}}
POST /data with form data email=bob@example.com
Response: {"user_query":"No user","email_form":"bob@example.com","json_body":{}}
POST /data with JSON {"age":30}
Response: {"user_query":"No user","email_form":"No email","json_body":{"age":30}}
Common Pitfalls
Common mistakes when getting request data in Flask include:
- Trying to access
request.jsonwhen the content type is not JSON, which returnsNone. - Using
request.formfor JSON data or vice versa. - Not specifying the correct HTTP methods in the route decorator, causing data not to be received.
- Forgetting to import
requestfromflask.
python
from flask import Flask, request app = Flask(__name__) # Wrong: expecting JSON but content type is form @app.route('/wrong', methods=['POST']) def wrong(): data = request.json # Will be None if content-type is not application/json return f"Data: {data}" # Right: check content type or use form for form data @app.route('/right', methods=['POST']) def right(): if request.is_json: data = request.json else: data = request.form.to_dict() return f"Data: {data}"
Quick Reference
| Request Data Type | Access Method | Description |
|---|---|---|
| URL Query Parameters | request.args | Dictionary of key-value pairs from URL query string |
| Form Data | request.form | Dictionary of form fields submitted via POST |
| JSON Data | request.json | Parsed JSON object from request body |
| Headers | request.headers | Dictionary of HTTP headers |
| Raw Data | request.data | Raw bytes of request body |
Key Takeaways
Use request.args for URL query parameters in GET requests.
Use request.form to get form data submitted via POST.
Use request.json to access JSON payloads; ensure content type is application/json.
Always import request from flask before accessing request data.
Check request method and content type to avoid getting None or errors.