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?
On September 26-27, the "2022 China Cloud Ne...
JustVPS.pro recently launched a new VPS in London...
Today, the China Academy of Information and Commu...
Confidentiality is a common practice in the data ...
The Industrial Internet is a network that connect...
Since the advent of mobile communications, humans...
[[398458]] In the intranet environment, there was...
[51CTO.com original article] On November 14, 2020...
[[269494]] This article will focus on exploring a...
According to foreign media reports, despite the o...
Bandwagonhost has restocked the special annual pa...
[Beijing, China, September 27] Today, the 2022 Ch...
Fans who love to think, have you ever had these c...
Recently, 16WiFi, which provides free WiFi Intern...