FastAPI Fun: A Tale of Passing Parameters and the Lessons from My Internship

Menglu Tao
3 min readJul 26, 2023

--

Photo by Rubaitul Azad on Unsplash

Hey there, fellow developers! Today, let’s take a thrilling journey through the enchanting realm of FastAPI and explore the exciting ways to pass parameters to our API endpoints.

As a beginner programmer, I’ve had the privilege of embarking on an exciting internship journey, and along the way, I’ve stumbled upon some common mistakes and valuable lessons. So buckle up, and let’s dive into the world of FastAPI magic!

  1. Path Parameters: Straight to the Point!
    Picture this: you’re building a RESTful API, and you’ve got these simple, short parameters that just need to hop on the URL read. Well, that’s where path parameters come to the rescue! These little fellas are part of the URL path, and you’ll spot them by those cute curly braces `{}`.
@router.post("/read/{org_id}/{platform_type}/{entity_id}/")
async def read(org_id: str, platform_type: str, entity_id: str) -> Any:
# Function magic here

Imagine you’re sending a POST request to `/read/apple/pear/grape/` — the parameters will be right there in the URL, ready to party!

Common Mistake:

One common mistake I made during my internship was forgetting to include the correct number of path parameters in the route URL. FastAPI is quite strict, and if the number of parameters in the URL doesn’t match the function’s arguments, you’ll end up with a pesky 404 Not Found error. So always double-check those curly braces!

Pros:
- Parameters are directly visible in the URL, easy peasy to read and construct.
- Perfect for simple, short parameters.

Cons:
- Things might get messy if you have tons of parameters, and their order can be puzzling.

2. Request Body or Query Parameters: Pydantic Sorcery!
Now, what if your parameters are a bit more complex and you want to keep that URL looking tidy? Say hello to the Pydantic magic! You can use Pydantic models for request bodies or query parameters, giving you a clean and organized API dance floor!

class ReadingParameters(BaseModel):
org_id: str
platform_type: str
entity_id: str


@router.post("/read/")
async def read(params: ReadingParameters) -> Any:
# Function sorcery here

How to Use (Postman Fun):
1. For Query Parameters:
URL: http://0.0.0.0:8088/read/?org_id=apple&platform_type=pear&entity_id=grape
HTTP Method: POST

2. For Request Body:
URL: http://0.0.0.0:8088/read/
HTTP Method: POST
Body: {
“org_id”: “apple”,
“platform_type”: “pear”,
“entity_id”: “graph”
}

Common Mistake:

When using Pydantic models, I initially struggled with incorrect field names or data types in the JSON request body. These mistakes often led to the dreaded 422 Unprocessable Entity error. Remember to double-check the field names and data types in your Pydantic model to avoid this issue.

Pros:
- No more URL clutter! Perfect for complex or lengthy parameters.
- Pydantic models validate like superheroes, keeping your API robust and well-documented.

Cons:
- Requires a bit more effort to construct requests with a request body, but it’s worth the magic!

Conclusion:
As a beginner programmer, my internship with FastAPI has been a rollercoaster of excitement and learning. Embracing the magic of passing parameters in FastAPI has opened doors to creating powerful APIs with ease. From understanding the art of path parameters to unlocking the Pydantic sorcery, I’ve learned to avoid common mistakes and make my API dance truly delightful.

So, my fellow apprentice wizards of FastAPI, go forth, build fantastic APIs, and let the FastAPI fun continue! Happy coding! 🚀🔥

--

--

Menglu Tao

Kicking off my career as a Software Engineer in the EU! I'll be sharing my personal journey as a first-gen woman, so stay tuned! 🚀😊