Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to retrieve GET vars in python bottle app

Tags:

I'm trying to make a simple REST api using the Python bottle app. I'm facing a problem in retrieving the GET variables from the request global object. Any suggestions how to retrieve this from the GET request?

like image 695
Maanas Royy Avatar asked Oct 23 '12 21:10

Maanas Royy


2 Answers

They are stored in the request.query object.

http://bottlepy.org/docs/dev/tutorial.html#query-variables

It looks like you can also access them by treating the request.query attribute like a dictionary:

request.query['city'] 

So dict(request.query) would create a dictionary of all the query parameters.

As @mklauber notes, this will not work for multi-byte characters. It looks like the best method is:

my_dict = request.query.decode() 

or:

dict(request.query.decode()) 

to have a dict instead of a <bottle.FormsDict object at 0x000000000391B...> object.

like image 94
Nathan Villaescusa Avatar answered Sep 21 '22 06:09

Nathan Villaescusa


If you want them all:

from urllib.parse import parse_qs  dict = parse_qs(request.query_string) 

If you want one:

one = request.GET.get('one', '').strip() 
like image 37
f p Avatar answered Sep 20 '22 06:09

f p