FastApi Tutorial 2 (Path parameters)
Path parameters
You can give parameters to apis in the same format as in standard python:
from fastapi import FastAPI
app = FastAPI()
@app.get("/items/{item_id}")
async def get_item(item_id):
return {"item_id": item_id}If you run the example: http://127.0.0.1:8000/items/foo , you will get the following response:
{"item_id":"foo"}Path parameters with types
You can declare the type of a path parameter using standard python type annotation:
from fastapi import FastAPI
app = FastAPI()
@app.get("/items/{item_id}")
async def get_item(item_id: int):
return {"item_id": item_id}In this case, item_id is declared as an int.
If you run http://127.0.0.1:8000/items/1 , you will get
{"item_id":3}If you run http://127.0.0.1:8000/items/foo , you will get a nice http error:
{
"detail": [
{
"type": "int_parsing",
"loc": [
"path",
"item_id"
],
"msg": "Input should be a valid integer, unable to parse string as an integer",
"input": "foo"
}
]
}The same error would appear if you provided float instead of int, http://127.0.0.1:8000/items/4.2
With the same python declaration FastApi gives you a data validation showing where exactly error happened which is nice when debugging.
Order matters
Let’s say we’ve “/items/me” and “/items/{item_id}”:
from fastapi import FastAPI
app = FastAPI()
@app.get("/items/me")
async def get_item_me():
return {"item_id": 1}
@app.get("/items/{item_id}")
async def get_item(item_id: int):
return {"item_id": item_id}In FastApi path operations are evaluated in order, that’s why “/items/me” must come before “/items/{item_id}”. Otherwise when we try to reach “/items/me”, “/items/{item_id}” will take the request and return error saying “Input should be a valid integer…”.
Predefined values
from enum import Enum
from fastapi import FastAPI
app = FastAPI()
class ModelName(Enum):
alexnet = "alexnet"
resnet = "resnet"
lenet = "lenet"
@app.get("/models/{model_name}")
async def get_model(model_name: ModelName):
return {"model_name": model_name}This way our get_model endpoint can only accept either [“alexnet”, “resnet”, “lenet”]. If other string is provided as a path parameter, we get an error:
Get Erlan Akbaralievich’s stories in your inbox
Join Medium for free to get updates from this writer.
http://127.0.0.1:8000/models/my_model
{
"detail": [
{
"type": "enum",
"loc": [
"path",
"model_name"
],
"msg": "Input should be 'alexnet', 'resnet' or 'lenet'",
"input": "my_model",
"ctx": {
"expected": "'alexnet', 'resnet' or 'lenet'"
}
}
]
}If everything is good, http://127.0.0.1:8000/models/alexnet :
{
"model_name": "alexnet"
}If you are wondering, “AlexNet”, “ResNet”, and “LeNet” are just names of Machine Learning models.
http://127.0.0.1:8000/doc Swagger UI
If you open the above url, you can interact with the API in a more easier to understand way.
Path parameters containing paths
Let’s say you have a path operation with a path /files/{file_path}.
But you need file_path itself to contain a path, like home/johndoe/myfile.txt.
So, the url for that file would be something like: /files/home/johndoe/myfile.txt







