When the client accesses the server program through the URL, it will send two types of information to the server, one is the HTTP request header, and the other is the request data. Generally, HTTP requests submit data to the server through the GET method and the POST method. Therefore, the server program needs to obtain the request data from the client and then do further processing. For example, if the server wants to do some statistics on the type of client (what browser is used), it needs to obtain the value of the User-Agent field in the HTTP request header. If you want to get the data submitted by the client form, you need to obtain the data of the GET request or POST request on the server. Reading POST requests will be introduced in detail in the following articles. This article will first look at how to read HTTP request headers and GET request data. To read HTTP request headers and GET request data in Flask, you need to import a global variable request in the flask module, and then use request.headers.get(...) to read HTTP request header data. The parameter of the get method is the name of the HTTP request header field. Use request.args.get(...) to read the value of a field in a GET request. The parameter value of the get method is the field name of the GET request. This example writes two routes, one for reading HTTP request header data and the other for reading GET request data.
Run the program and enter the following URL in the browser address bar.
After accessing the above URL, the information shown in Figure 1 will be output in the browser. It should be noted that this output information will vary depending on the browser used by the reader. But it will describe the type of browser used by the reader. For example, this example uses the Chrome browser for testing, so the output information will show Chrome and the corresponding version number. Figure 1 Returning the browser type Next, enter the following URL in the browser address bar.
The content shown in Figure 2 will be output in the browser. Figure 2 Returning GET request parameter values This article is reprinted from the WeChat public account "Geek Origin", which can be followed through the following QR code. To reprint this article, please contact the Geek Origin public account. |
<<: What will 6G look like in the future?
>>: Do you know all the HTTP protocols?
introduction Hello, everyone. I am your technical...
[[396354]] This article is reprinted from the WeC...
Why's THE Design is a series of articles abou...
CUBECLOUD has launched a limited-time promotion d...
[[403098]] The development of 5G healthcare has e...
If the Industrial Internet is to be implemented, ...
The "old-fashioned" old boss—SDH The tr...
After a complicated 2020, the wheel of time has e...
At the beginning of the year, we shared informati...
666clouds recently launched a three-year annivers...
In campus networks, there are many emerging trend...
HostYun has added a new VPS product in Hong Kong&...
On the occasion of the Chinese New Year, the trib...
1. Classification of optical fiber Optical fibers...