[Flask] Flask HTTP method and Request object

Flask HTTP method

Flask HTTP method

Http protocol is the basis of data communication in the World Wide Web. Different methods of retrieving data from the specified URL are defined in the protocol.

The following table summarizes the different http methods:

Serial number Method and description
1

GET

Send the data to the server in unencrypted form. The most common method.

2

HEAD

Same as GET method, but without response body.

3

POST

Used to send HTML form data to the server. The data received by the POST method is not cached by the server.

4

PUT

Replace all current representations of the target resource with the uploaded content.

5

DELETE

Delete all current representations of the target resource given by the URL.

By default, Flask routing responds to GET requests. However, this preference can be changed by providing method parameters to the route() decorator.

To demonstrate the use of the POST method in URL routing , let us first create an HTML form and use the POST method to send the form data to the URL.

Save the following script as login.html

<html>
   <body>
      
      <form action = "http://localhost:5000/login" method = "post">
         <p>Enter Name:</p>
         <p><input type = "text" name = "nm" /></p>
         <p><input type = "submit" value = "submit" /></p>
      </form>
      
   </body>
</html>

Now enter the following script in the Python shell:

from flask import Flask, redirect, url_for, request
app = Flask(__name__)

@app.route('/success/<name>')
def success(name):
   return 'welcome %s' % name

@app.route('/login',methods = ['POST', 'GET'])
def login():
   if request.method == 'POST':
      user = request.form['nm']
      return redirect(url_for('success',name = user))
   else:
      user = request.args.get('nm')
      return redirect(url_for('success',name = user))

if __name__ == '__main__':
   app.run(debug = True)

After the development server is running, open login.html in the browser , enter name in the text field, and click Submit .

Flask HTTP method

The form data will be POSTed to the URL in the action clause of the form tag.

http://localhost/login maps to the login() function. Since the server receives data through the POST method, the value of the "nm" parameter obtained from the form data is obtained through the following steps:

user = request.form['nm']

It is passed to the '/success'  URL as the variable part . The browser displays the welcome message in the window .

Flask HTTP method

Change the method parameter to'GET ' in login.html , and then open it again in the browser. The data received on the server is obtained through the GET method. Obtain the value of the'nm' parameter through the following steps:

User = request.args.get(‘nm’)

Here, args is a dictionary object containing a list of form parameter pairs and their corresponding value pairs. The value corresponding to the'nm' parameter will be passed to the'/ success' URL as before.

Flask Request object

The data from the client web page is sent to the server as the global request object. In order to process the request data, it should be imported from the Flask module.

The important attributes of the Request object are listed below:

  • Form  -It is a dictionary object containing key and value pairs for form parameters and their values.

  • args  -Parse the content of the query string, which is part of the URL after the question mark (?).

  • Cookies   -A dictionary object that stores cookie names and values.

  • files  -data related to uploaded files.

  • method  -the current request method.

Guess you like

Origin blog.csdn.net/u013066730/article/details/108243429
Recommended