I'm wondering how to go about obtaining the value of a POST/GET request variable using Python with Flask.
With Ruby, I'd do something like this:
variable_name = params["FormFieldValue"]
How would I do this with Flask?
To access the incoming data in Flask, you have to use the request object. The request object holds all incoming data from the request, which includes the mimetype, referrer, IP address, raw data, HTTP method, and headers, among other things.
In the first one we would use request. args. get('<argument name>') where request is the instance of the class request imported from Flask. Args is the module under which the module GET is present which will enable the retrieve of the parameters.
By default, the Flask route responds to GET requests. However, you can change this preference by providing method parameters for the route () decorator. To demonstrate the use of a POST method in a URL route, first let us create an HTML form and use the POST method to send form data to the URL.
If you want to retrieve POST data:
first_name = request.form.get("firstname")
If you want to retrieve GET (query string) data:
first_name = request.args.get("firstname")
Or if you don't care/know whether the value is in the query string or in the post data:
first_name = request.values.get("firstname")
request.values is a CombinedMultiDict that combines Dicts from request.form and request.args.
You can get posted form data from request.form
and query string data from request.args
.
myvar = request.form["myvar"]
myvar = request.args["myvar"]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With