From the course: Build REST APIs with FastAPI

Unlock this course with a free trial

Join today to access over 24,200 courses taught by industry experts.

Query parameters

Query parameters

One of the simplest way to pass parameters to the server is via query parameters. And you're probably familiar with it. You access /logs, for example, and you specify the start, the end, and the level. So the query parameters start with a question mark. And then between them, there's the ampersand. Let's see how it looks in FastAPI. So I'm importing HTTP status because I like to use the status and not the number. And I'm going to use datetime. And I'm importing FastAPI and the HTTP exception for FastAPI, which I'm going to use for invalid parameters. I import the logs. This is the package with the dummy database that I wrote. You can have a look at the code later if you want. And I'm creating my app. And then I'm saying that this handler is for /logs. And then I define the logs query. And what I'm doing is I'm giving a parameter for every query parameter there is. So the start, the end, and the level. And I'm giving them a type hint. So FastAPI knows how to translate from the string…

Contents