In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "Flask request object example analysis". In daily operation, I believe many people have doubts about Flask request object example analysis. The editor consulted all kinds of data and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "Flask request object example analysis". Next, please follow the editor to study!
Foreword:
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 easily access the request and response message information, the Flask framework provides some built-in objects. Let's talk about the built-in object request provided by Flask for the request.
1. Request request object
The request request object encapsulates the request message information sent from the client, and we can get all the data in the request message from the request object. Most of its functions are accomplished by the dependency package Werkzeug, and Flask encapsulates some specific functions to form a request request object.
Use of 1.1request request object
Import the request object in the flask package, and you can use it directly in the request function.
From flask import request@app.route ('/ user', methods= ['GET',' POST']) def user (): if request.method = = 'POST': user_name = request.form [' user_name'] return 'user: {} add original.format (user_name) else: user_id = request.args.get (' user_id', 0) return 'Hello user: {}!' .format (user_id)
In the above code: the method variable in the request object can get the current request method, that is, GET, POST, PUT, DELETE, etc.; form variable to obtain the data in the POST request form-data form, which is essentially a dictionary, if there is no user_name in the submitted form, it will return a 400 Bad Request error, of course, you can also use the exception catch mechanism to handle. The request.args.get () method gets the parameters in the url of the GET request (question mark? The first parameter specifies the key of the acquired url parameter, and the second parameter specifies the default value, which is returned if the key does not exist.
As follows:
GET request
POST request
In addition, other information in the request message can be obtained through the properties and methods provided by the request object. The common parts are as follows:
Url: the requested url
The ImmutableMultiDict object of args:Werkzeug, which stores the parsed query string, and the key value can be obtained by dictionary.
Blueprint: the name of the current blueprint
Cookies: a dictionary containing all the cookies submitted with the request
Data: contains request data in string form
Endpoint: the endpoint value that matches the current request
The MultiDict object of files:Werkzeug, which contains all uploaded files
The ImmutableMultiDict object of form:Werkzeug, which contains the parsed form data
The CombinedMultiDict object of values:Werkzeug, which combines the values of the args and form properties
Get_data (cache=True,as_text=False,parse_from_data=False): gets the data in the request. It is read as a bytestring by default. If as_text is True, the decoded unicode string is returned.
Get_json (self,force=False,silent=False,cache=True): parses and returns data as json, returns None if MIME type is not json (unless force is set to True), throws BadRequest exception provided by Werkzeug if parsing error (returns 400error response if debug mode is not enabled), returns None;cache setting whether parsed json data is cached if debug mode is set to True
The EnvironHeaders object of headers:Werkzeug, which contains the header field of the request
Json: contains parsed json data. Get_json () is called internally, and the key value can be obtained by dictionary.
Method: HTTP method of the request
Referrer: the source url of the request, that is, referer
Scheme: the requested URL mode (http or https)
User_agent: user agent (User Agent), which contains the client type, operating system type and other information of the user.
At this point, the study of "Flask request object example Analysis" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.