Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use query string in Falcon python

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?

like image 992
woss Avatar asked Feb 05 '23 04:02

woss


2 Answers

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.

like image 137
Max Gasner Avatar answered Feb 06 '23 19:02

Max Gasner


You can use falcon.uri module to parse query string.

qs = falcon.uri.parse_query_string(req.query_string)
        if "myq" in qs:
            searchedQ = qs["myq"]
like image 35
Narges Mousavi Avatar answered Feb 06 '23 19:02

Narges Mousavi