how to use params dict in a "get" request #10583
-
First Check
Commit to Help
Example Codefrom fastapi import FastAPI, Path
app = FastAPI()
items = [{"foo": "bar"}, {"john": "doe"}]
@app.get("/item/{item_id}",
description="get item by id",
tags=["test"])
async def read_item(item_id: int = Path(..., description="the id of the item to get")):
return {"item": items[item_id]}DescriptionHi, I've been trying to use this endpoint in a request by doing: I tried to add the header as well ( thanks in advance Operating SystemmacOS Operating System DetailsNo response FastAPI Version0.104.0 Pydantic Version2.4.2 Python VersionPython 3.11.4 Additional ContextNo response |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
|
When you write this requests is going to create a request to but you have told FastAPI that you want item id from the path by putting it in the brackets in the path @app.get("/item/{item_id}",...which is a request like this If you want the endpoint to work like your example request you need to use query params https://fastapi.tiangolo.com/tutorial/query-params/#query-parameters |
Beta Was this translation helpful? Give feedback.
When you write this
requests is going to create a request to
http://127.0.0.1:8000/item?item_id=0but you have told FastAPI that you want item id from the path by putting it in the brackets in the path
which is a request like this
http://127.0.0.1:8000/item/0If you want the endpoint to work like your example request you need to use query params https://fastapi.tiangolo.com/tutorial/query-params/#query-parameters