falconry / falcon

The no-magic web data plane API and microservices framework for Python developers, with a focus on reliability, correctness, and performance at scale.
https://falcon.readthedocs.io/en/stable/
Apache License 2.0
9.53k stars 945 forks source link

Falcon request, doesn't parse a GET array properly #2065

Closed 0mars closed 2 years ago

0mars commented 2 years ago

example url:

http://0.0.0.0:8025/v1/properties?q=&district=Lighthouse&offset=0&pricefrom=0&priceto=10000000&renttype=weekly&limit=10&bedrooms=1&benefits[]=wifi&benefits[]=ac

it reads the argument as benefits[] = 'ac'

but it should be a list of values instead

req.params['benefits'] = None

CaselIT commented 2 years ago

Hi,

It's working as expected for me. Did you use get_param instead of get_param_as_list? As documented returns a single value

Here an example:

import falcon
class F:
    def on_get(self, req: falcon.Request, res: falcon.Response):
        res.media = {
            'params': req.params,
            'get_param': req.get_param('benefits[]'),
            'get_param_as_list': req.get_param_as_list('benefits[]')
        }

app = falcon.App()
app.add_route('/route', F())
>>> xh  ":8080/route?q=&district=Lighthouse&offset=0&pricefrom=0&priceto=10000000&renttype=weekly&limit=10&bedrooms=1&benefits[]=wifi&benefits[]=ac"
HTTP/1.1 200 OK
Content-Length: 243
Content-Type: application/json
Date: Sat, 14 May 2022 08:31:56 GMT
Server: waitress

{
    "params": {
        "q": "",
        "district": "Lighthouse",
        "offset": "0",
        "pricefrom": "0",
        "priceto": "10000000",
        "renttype": "weekly",
        "limit": "10",
        "bedrooms": "1",
        "benefits[]": [
            "wifi",
            "ac"
        ]
    },
    "get_param": "ac",
    "get_param_as_list": [
        "wifi",
        "ac"
    ]
}
vytas7 commented 2 years ago

Closing this since we cannot see any issue in the framework apart from the fact that Falcon doesn't treat the trailing [] in a parameter name in any special way. Although I've seen this [] in some APIs, but I think it's more common, or at least as common, to simply write ?benefits=wifi&benefits=ac (i.e., without the []). Furthermore, as @CaselIT demonstrated above, handling this [] should be fairly straightforward in your app's logic.