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?
Have you ever found yourself always experiencing ...
DiyVM is a Chinese hosting company founded in 200...
According to the 21st Century Business Herald, &q...
[[420148]] Introduction There are already multipl...
Web-based instant messaging The server can immedi...
Enterprises need to develop an effective and adap...
[[349454]] 5G will fundamentally change the way o...
At the MWC 2017 conference, which has ended, peop...
[51CTO.com original article] On June 26, the Wi-F...
Today I am going to introduce the open source API...
1. Routing Table When the frame reaches the route...
[[411111]] background The hottest topic in the do...
Following the 5G Summit in September and the rele...
When it comes to IP addresses (IPv4), common IP a...
Recently, China Telecom Gansu Company held a pres...