Search code examples
pythonroutessanic

Sanic - path type of endpoint doesn't include query arguments


If I have defined my route as (only relevant part of code):

...
@app.get("brawlstats/endpoint:path>")
...

and I try to access route /brawlstats/rankings/global/brawlers/16000001?limit=200, it cuts off at the question mark. I want endpoint to include the entire url. How do I make it include the whole path ?


Solution

  • Everything after ? is considered as part of query string. Your endpoint represents path after /brawlstats.

    Since endpoint and limit argument are split apart, you can retrieve both with this example:

    from sanic import Sanic
    from sanic.response import text
    
    app = Sanic()
    
    
    @app.get('/brawlstats/<endpoint:path>')
    async def brawlstats(request, endpoint):
    
        # endpoint is set to part after '/brawlstats'.
        # limit query arg is converted to integer or 0 if it's not set.
        limit = int(request.args.get('limit', 0))
    
        if endpoint == 'rankings/global/brawlers/16000001' and limit > 0:
            return text('Success', 200)
    
        return text('Bad request', 400)
    
    if __name__ == "__main__":
        app.run(host="127.0.0.1", port=8000)