SoFunction
Updated on 2025-04-27

Summary of how to get request parameters using Flask

In Flask, various types of parameters can be obtained from the request object. The following is a comprehensive way to obtain parameters and sample code.

1. Get the URL query parameters (Query String Parameters)

The query parameters in the URL are passed in the form of ?key=value&key2=value2 and obtained using .

Example:

from flask import Flask, request

app = Flask(__name__)

@('/query')
def get_query_params():
    param1 = ('param1', default=None, type=str)  # Get a single parameter    param2 = ('param2')  # Get multiple parameters of the same name    return f"param1: {param1}, param2: {param2}"

# Test URL: http://127.0.0.1:5000/query?param1=value1&param2=value2&param2=value3

2. Get form data (Form Data)

When the request method is POST or PUT, form data can be obtained through .

Example:

@('/form', methods=['POST'])
def get_form_data():
    name = ('name', default=None, type=str)  # Get a single parameter    age = ('age', default=0, type=int)  # Get and convert to integer    return f"name: {name}, age: {age}"

# Test: Submit form data using Postman or curl

3. Get JSON data

When the client sends data in JSON format, you can use to get the parsed dictionary.

Example:

@('/json', methods=['POST'])
def get_json_data():
    data =   # Get JSON data    name = ('name', None)
    age = ('age', 0)
    return f"name: {name}, age: {age}"

# Test: Submit JSON data using Postman or curl# Request header: Content-Type: application/json# Request body: {"name": "Alice", "age": 25}

4. Get File Uploads

When uploading a file, you can obtain the file object through.

Example:

@('/upload', methods=['POST'])
def upload_file():
    file = ('file')  # Get uploaded files    if file:
        filename = 
        (f"./{filename}")  # Save the file to the local        return f"File uploaded: {filename}"
    return "No file uploaded"

# Test: Submit files using Postman or curl

5. Get the path parameters (Route Parameters)

The path parameters are dynamic parts defined by the URL path, defined using <variable_name>, and received in the function.

Example:

@('/user/&lt;username&gt;')
def get_user(username):
    return f"Hello, {username}!"

# Test URL: http://127.0.0.1:5000/user/Alice

6. Get the original request body (Raw Body)

If you need to directly get the original content of the request, you can use or request.get_data().

Example:

@('/raw', methods=['POST'])
def get_raw_data():
    raw_data = ('utf-8')  # Get and decode the original data    return f"Received raw data: {raw_data}"

# Test: Submit raw data in any format

7. Get Headers

Get the request header information through.

Example:

@('/headers')
def get_headers():
    user_agent = ('User-Agent')  # Get User-Agent    host = ('Host')  # Get Host    return f"User-Agent: {user_agent}, Host: {host}"

# Test: Access any URL

8. Get Cookies

Get the cookies sent by the client.

Example:

@('/cookies')
def get_cookies():
    session_id = ('session_id', default=None)  # Get Cookies    return f"Session ID: {session_id}"

# Test: Set cookies and access

9. Get the Method type

Get the method type of the currently requested (such as GET, POST, etc.).

Example:

@('/method', methods=['GET', 'POST'])
def get_method():
    return f"Request method: {}"

# Test: Access using GET and POST methods respectively

10. Get the full URL

Get the full request URL by

Example:

@('/url')
def get_full_url():
    return f"Full URL: {}"

# Test: Access any URL

Summarize

Way Use scenarios Properties/Methods
Query parameters Key-value pairs in URL
Form data HTML Form Submission
JSON data Client sends JSON format data
File upload File upload
Path parameters Parameters in dynamic routing Function parameters
Original request body Get unresolved request body
Headers Get request header information
Cookies Get the cookies sent by the client
Method Type Get the request method type
Full URL Get the full request URL

The above methods cover Flask fromrequestThe main way to obtain parameters in an object, just choose the appropriate method according to actual needs.

This is the article summarizing the method of using Flask to obtain request parameters. For more relevant content on obtaining request parameters, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!