Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the named parameters from a URL using Flask?

When the user accesses this URL running on my flask app, I want the web service to be able to handle the parameters specified after the question mark:

http://10.1.1.1:5000/login?username=alex&password=pw1  #I just want to be able to manipulate the parameters @app.route('/login', methods=['GET', 'POST']) def login():     username = request.form['username']     print(username)     password = request.form['password']     print(password) 
like image 969
Alex Stone Avatar asked Jul 22 '14 15:07

Alex Stone


People also ask

How do I access URL parameters in Flask?

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.

How can I get parameters from a URL string?

The parameters from a URL string can be retrieved in PHP using parse_url() and parse_str() functions. Note: Page URL and the parameters are separated by the ? character. parse_url() Function: The parse_url() function is used to return the components of a URL by parsing it.

How do you find the variable in a URL?

To add a URL variable to each link, go to the Advanced tab of the link editor. In the URL Variables field, you will enter a variable and value pair like so: variable=value. For example, let's say we are creating links for each store and manager.


1 Answers

Use request.args to get parsed contents of query string:

from flask import request  @app.route(...) def login():     username = request.args.get('username')     password = request.args.get('password') 
like image 130
falsetru Avatar answered Sep 24 '22 12:09

falsetru