6

In Python and GAE, I would like to ask how to get the parameters of a query string in the url. As I know, the query_string part returns all the part after the "?" in the url. So what I have to do is to split the query string with "&", and use the variables. Is there any other convinient way to manage the query string? How do you normally do it?

str_query = self.request.query_string
m = str_query.split('&')
a = m[0] 
b = m[1]
c = m[2]

Doing this way, in case, the query_string does not have any values, it threw an error:

IndexError: list index out of range

2 Answers 2

18

You don't need to complicate. You can retrieve all GET parameters with:

self.request.get('var_name')

Or if you want to retrieve them all in one list you can use:

self.request.get_all()

You can find more info on the Request class here.

Sign up to request clarification or add additional context in comments.

2 Comments

self.request.get_all() doesn't actually work without a parameter; it doesn't return all the query variables, it just returns all the values of a particular variable. For the complete list you need to use self.request.arguments().
As @apenwarr says, this is no longer a valid answer: get_all needs a parameter name to get all values for
0

If you want to iterate over all the parameters of your request, you should do something like this:

for argument in self.request.arguments():
    values = self.request.get_all(argument)
    # do something with values (which is a list)

Or, you could build your own dict containing all the data:

params = {arg: self.request.get_all(arg) for arg in self.request.arguments()}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.