[Python] How to allow any arbitrary query parameters using FastAPI and Swagger?

Discussão em 'Python' iniciado por Stack, Junho 6, 2025.

  1. Stack

    Stack Membro Participativo

    Note: This question is different from the one here, in that I need it to work with Swagger.

    Given a FastAPI GET endpoint, I want to allow any arbitrary set of URL parameters, while maintaining Swagger support.

    My use case is that I want to support a JSON API-like set of query parameters such as this:

    /api/books/?include=author&sort=name,zip&sort[author]=-lname&fields=name,phone,street


    The use of square brackets prevents me from using traditional classes to model query parameters, so I'm directly using the Request object instead. However, I would like to use Swagger to test the endpoint. I can't find a way to provide arbitrary URL parameters. I'm happy to type them in as a single string.

    One would think something like the following:

    def books(**params):
    ....


    That gives a curl statement of:

    api/books?params=sort%5Bone%5D%3Dtwo'


    What I really want is:

    api/books?sort&one%5D%3Dtwo'

    Continue reading...

Compartilhe esta Página