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?
4 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.

- 41,386
- 99
- 383
- 673

- 17,331
- 4
- 53
- 56
-
3Note, based on the docs, dict based access does not properly decode and renecode multi-byte characters. – mklauber Oct 23 '12 at 21:30
-
Ah, yeah it seems like `my_dict = request.query.decode()` is correct way. – Nathan Villaescusa Oct 23 '12 at 21:33
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()
-
-
1@f p: it returns an empty string as well as your code. The only difference is that request.query.one is always a Unicode string. – jfs Oct 24 '12 at 11:36
Can you try this please:
For this example : http://localhost:8080/command?param_name=param_value
In your code:
param_value = request.query.param_name

- 31
- 2
from the docs
name = request.cookies.name
# is a shortcut for:
name = request.cookies.getunicode('name') # encoding='utf-8' (default)
# which basically does this:
try:
name = request.cookies.get('name', '').decode('utf-8')
except UnicodeError:
name = u''
So you might prefer using attribute accessor (request.query.variable_name) than request.query.get('variable_name')
Another point is you can use request.params.variable_name which works both for GET and POST methods, than having to swich request.query.variable_name or request.forms.variable_name depending GET/POST.

- 3,092
- 5
- 25
- 41