This article introduces the Flask request object, a complete HTTP request, including the Request request sent by the client to the server and the Response response sent by the server. In order to facilitate access to request and response message information, the Flask framework provides some built-in objects, Let's talk about Flask's built-in object requests for requests. Friends who need it can refer to it
Must-know Flask request Flask Request object
Data from the client web page is sent to the server as a global request object. In order to process request data, it should be imported from the Flask module.
Important properties of the Request object are listed below:
form − It is a dictionary object containing key and value pairs of form parameters and their values.
args - The content of the parsed query string, which is the part of the URL after the question mark (?).
Cookies - A dictionary object holding cookie names and values.
files - Data related to uploaded files.
method - the current request method
It is also relatively simple to use request, which is to import the request object directly from the flask module:
from flask import request
1 2 3 4 5 6 7 8 9 |
|
You must know that Flask's Cookies obtain data Cookies are stored on the client's computer in the form of text files.
Its purpose is to remember and track data related to customer usage for better visitor experience and website statistics.
Flask's Request object contains Cookie properties.
It is a dictionary object of all cookie variables and their corresponding values
Among other things, the cookie stores its expiry time, path and domain name of the website.
In Flask, the processing steps for cookies are:
1 set cookies:
Set a cookie, the default validity period is a temporary cookie, and it will be invalid when the browser is closed
You can set the validity period through max_age, and the unit is seconds
2 get cookies
To get cookies, through request.cookies, a dictionary is returned, and the corresponding values in the dictionary can be obtained
3 delete cookies
The deletion here is just to expire the cookie, not to delete the cookie directly
To delete a cookie, use delete_cookie(), which contains the name of the cookie
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
|
Flask file uploads Handling file uploads in Flask is very simple.
All that is required is an HTML form with the enctype attribute set to 'multipart/form-data' to post the file to a URL.
The URL handler extracts the files from the request.files[] object and saves them to the desired location
1 2 3 4 5 6 7 8 9 10 11 12 |
|
Each uploaded file is first saved in a temporary location on the server
Then actually save it to its Y end position.
The name of the target file can be hardcoded
It can also be obtained from the filename attribute of the request.files[file] object.
However, it is recommended to use the secure_filename() function to get a secure version of it
The path of the default upload folder and the Y value of the uploaded file are defined in the configuration settings of the Flask object
app.config['UPLOAD_FOLDERʼ] defines the path of the upload folder
app.config['MAX_CONTENT_LENGTHʼ] specifies the Y maximum value (in bytes) of the file to be uploaded
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
|
This is the end of this article about the detailed use of Python Flask's request object.
Click to get
50G+ learning video tutorials
100+Python elementary, intermediate and advanced e-books