杰瑞科技汇

Flask request对象如何获取请求参数?

Of course! The request object is one of the most fundamental and important components in Flask. It acts as a global object (but is actually a proxy object tied to a specific context) that gives you access to all the information about an incoming HTTP request.

Flask request对象如何获取请求参数?-图1
(图片来源网络,侵删)

Here’s a comprehensive guide to using the request object in Flask, from the basics to more advanced topics.


The Basics: Accessing the request Object

To use the request object, you first need to import it from flask.

from flask import Flask, request
app = Flask(__name__)
@app.route('/greet', methods=['GET', 'POST'])
def greet():
    # Now you can access request attributes
    # ... (code examples below)
    pass

Important Note: The request object is only available in the context of a request. You cannot access it outside of a view function. Flask uses a concept called Application Context and Request Context to manage this. You don't need to worry about the details, just remember to only use request inside a route handler.


Key Attributes of the request Object

The request object contains a wealth of information about the incoming request. Here are the most commonly used attributes.

Flask request对象如何获取请求参数?-图2
(图片来源网络,侵删)

A. Request Method

  • request.method: A string containing the HTTP method used for the request (e.g., 'GET', 'POST', 'PUT', 'DELETE').
@app.route('/method', methods=['GET', 'POST'])
def show_method():
    if request.method == 'POST':
        return f"This is a POST request."
    else:
        return f"This is a GET request."

B. Request Arguments (URL Parameters)

These are the key-value pairs in the URL after the .

  • request.args: An object that acts like a dictionary for the URL query string.
# URL: http://127.0.0.1:5000/search?query=flask&sort=newest
@app.route('/search')
def search():
    query = request.args.get('query', '') # Use .get() for safe access with a default
    sort = request.args.get('sort', 'relevance')
    if not query:
        return "Please provide a search query."
    return f"Searching for '{query}', sorted by '{sort}'."

Best Practice: Always use request.args.get('key', default_value) instead of request.args['key'] to avoid a KeyError if the parameter is not present.

C. Request Data (Form & JSON)

This is the data sent in the body of the request.

  • request.form: An object for accessing form data submitted via a POST request (with Content-Type: application/x-www-form-urlencoded or multipart/form-data).
  • request.data: Contains the raw, unparsed body data as a string/bytes object. Useful if the data is not in a standard format.
  • request.json: If the Content-Type header is application/json, this attribute contains the parsed JSON data as a Python dictionary.
@app.route('/profile', methods=['POST'])
def profile():
    # Handling form data
    name = request.form.get('name')
    email = request.form.get('email')
    # Handling JSON data
    # The client must send: Content-Type: application/json
    # And a body like: {"bio": "Python developer"}
    bio_data = request.get_json() # Safely parses JSON, returns None if not valid
    bio = bio_data.get('bio') if bio_data else "No bio provided."
    if name and email:
        return f"Profile for {name} ({email}). Bio: {bio}"
    else:
        return "Name and email are required in the form data.", 400

D. Request Headers

  • request.headers: An object that acts like a dictionary for all HTTP headers.
@app.route('/headers')
def show_headers():
    user_agent = request.headers.get('User-Agent')
    accept_language = request.headers.get('Accept-Language')
    return f"Your browser is: {user_agent}<br>You prefer languages like: {accept_language}"

E. Request Cookies

  • request.cookies: A dictionary containing all the cookies sent by the client.
@app.route('/get-cookie')
def get_cookie():
    username = request.cookies.get('username')
    if username:
        return f"Welcome back, {username}!"
    else:
        return "No cookie found. Please log in."

F. Other Useful Attributes

  • request.url: The full URL of the request.
  • request.base_url: The URL without the query string (e.g., http://127.0.0.1:5000/search).
  • request.path: The path part of the URL (e.g., /search).
  • request.files: A dictionary-like object containing uploaded files. The keys are the name attributes from the HTML <input type="file"> tags.

Full Example: A Simple Form Handler

Let's put it all together in a practical example. This app will have a page with a form, and it will process the form submission.

Flask request对象如何获取请求参数?-图3
(图片来源网络,侵删)

File: app.py

from flask import Flask, request, render_template, redirect, url_for
app = Flask(__name__)
# A page with a form
@app.route('/submit', methods=['GET'])
def show_form():
    return render_template('form.html')
# The handler for the form submission
@app.route('/submit', methods=['POST'])
def handle_submission():
    # 1. Get data from the form
    name = request.form.get('name')
    age = request.form.get('age')
    # Basic validation
    if not name or not age:
        return "Error: Name and age are required!", 400
    # 2. Get a header
    user_ip = request.headers.get('X-Forwarded-For', request.remote_addr)
    # 3. Process the data (in a real app, you'd save this to a database)
    message = f"Hello, {name}! You are {age} years old. Your IP is {user_ip}."
    # Redirect to a "thank you" page, passing the message as a URL parameter
    return redirect(url_for('thank_you', msg=message))
# A page to show after successful submission
@app.route('/thank-you')
def thank_you():
    msg = request.args.get('msg', 'Submission successful!')
    return f"<h1>Thank You!</h1><p>{msg}</p>"
if __name__ == '__main__':
    app.run(debug=True)

File: templates/form.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">Submit Form</title>
</head>
<body>
    <h1>Submit Your Information</h1>
    <form action="/submit" method="POST">
        <div>
            <label for="name">Name:</label>
            <input type="text" id="name" name="name" required>
        </div>
        <br>
        <div>
            <label for="age">Age:</label>
            <input type="number" id="age" name="age" required>
        </div>
        <br>
        <button type="submit">Submit</button>
    </form>
</body>
</html>

Best Practices & Security

  1. Never Trust User Input: Always validate and sanitize all data from request.form, request.args, and request.json.
  2. Use .get() for Arguments: As mentioned, request.args.get('key') is safer than request.args['key'] because it returns None if the key is missing, preventing a 500 error.
  3. Validate File Uploads: If you handle request.files, always validate the filename, its extension, and its size before saving it to disk to prevent security vulnerabilities.
  4. Use CSRF Protection: For forms that modify state (e.g., POST requests), you should protect against Cross-Site Request Forgery (CSRF) attacks. Flask-WTF is an extension that makes this easy.
  5. Be Aware of request.data: Only use request.data if you are expecting raw, unparsed data. For standard forms and JSON, prefer request.form and request.json as they handle the parsing for you.
分享:
扫描分享到社交APP
上一篇
下一篇