How to use query string in Falcon python

woss picture woss · Oct 19, 2017 · Viewed 7.8k times · Source

Ho do you configure the app to have following features:

  1. Have an api-endpoint defined as app.add_route('/v1/tablets', TabletsCollection())

  2. And still be able to use QueryParams like this https://example.com/api/v1/tablets?limit=12&offset=50&q=tablet name

the Collection has two methods on_get for retrieving the whole list and using the filter and on_post for the creation of a single record.

I've been searching the net for some time, how do you get query_string and properly parsed params?

Answer

Max Gasner picture Max Gasner · Oct 20, 2017

Both the query string and the parsed query string (as a dict) are available on the Request object that falcon passes to your API endpoint.

Your handler can do stuff like:

class TabletsCollection():
    def on_post(self, req, resp):
        print req.query_string
        for key, value in req.params.items():
            print key, value

See http://falcon.readthedocs.io/en/stable/api/request_and_response.html#Request.query_string and http://falcon.readthedocs.io/en/stable/api/request_and_response.html#Request.params.