Let’s build a showcase API!
REST APIs are all about cycles of HTTP requests and responses – it is the engine that powers the web and is implemented in every web framework, speaking the language of the web – the HTTP protocol. I feel that the best way to showcase FastAPI’s capabilities is to dive right in and create simple endpoints and focus on specific parts of code that achieve the desired functionalities. Rather than the usual CRUD operations that we will implement in the forthcoming chapters, I want to focus on the process of retrieving and setting request and response elements.
Retrieving path and query parameters
The first endpoint will be for retrieving a car by its unique ID:
chapter3_path.py
from fastapi import FastAPI app = FastAPI() @app.get("/car/{id}") async def root(id): return {"car_id":id}
The first line of the preceding snippet defines a dynamic path: the static part is defined with...