1. Query parameter definition
In the path operation function, it is either other parameters of the path parameter or query parameters. for example:
from fastapi import FastAPI app = FastAPI() @( "/items" ) def read_item(item_id:int,item_name:str): return { "item_id" : item_id,"item_name":item_name} if __name__ == "__main__" : import uvicorn ( ":app" ,reload=True ,port= 8001)
item_id and item_name are not path parameters, so it is just query parameters.
2. Query parameters function
Convenient to path operation function transfer parameters
3. Basic use of query parameters
3.1. URL splicing and required parameters
Query parameters, usually in the URL?
After that, and&
Split, for example:
from fastapi import FastAPI app = FastAPI() @( "/items" ) def read_item(item_id:int,item_name:str): return { "item_id" : item_id,"item_name":item_name} if __name__ == "__main__" : import uvicorn ( ":app" ,reload=True ,port= 8001)
at this timeitem_id
anditem_name
Both are required, and the URL must pass these two parameters. Start the program and access it in the browserhttp://127.0.0.1:8001/items?item_id=1&item_name=xiaoming
, the response is:
{"item_id":1,"item_name":"xiaoming"}
3.2. Default value
Query parameters can set default values, such as:item_name
The default value isxiaoming
from fastapi import FastAPI app = FastAPI() @( "/items" ) def read_item(item_id:int,item_name:str='xiaoming'): return { "item_id" : item_id,"item_name":item_name} if __name__ == "__main__" : import uvicorn ( ":app" ,reload=True ,port= 8001)
accesshttp://127.0.0.1:8001/items?item_id=1&item_name=xiaoming
and visithttp://127.0.0.1:8001/items?item_id=1
Their results are the same. Because it is not set in the URLitem_name
The value of , it will be filled with the default value.
3.3. Optional parameters
Query parameters can be set as optional parameters, such asitem_name=None
The meaning of optional parameters: This parameter can be not included in the URL, which is optional
from fastapi import FastAPI app = FastAPI() @( "/items" ) def read_item(item_id:int,item_name:str=None): if item_name: return { "item_id" : item_id,"item_name":item_name} else: return { "item_id" : item_id} if __name__ == "__main__" : import uvicorn ( ":app" ,reload=True ,port= 8001)
accesshttp://127.0.0.1:8001/items?item_id=1&item_name=xiaoming
The response is:
{"item_id":1,"item_name":"xiaoming"}
and visithttp://127.0.0.1:8001/items?item_id=1
The response is:
{"item_id":1}
3.4. Pydantic model (request body) as query parameters
from fastapi import FastAPI from pydantic import BaseModel app = FastAPI() class Item(BaseModel): name: str price: float @( "/items/{item_id}" ) def put_item(item_id:int,item:Item): return { "item_id" : item_id, "item" :item} if __name__ == "__main__" : import uvicorn ( ":app" , reload=True, port=8001)
Client:
import requests data={ "name" : "flow" , "price" :2.3} respone=( "http://127.0.0.1:8001/items/1" ,json=data) print(())
Result of response:
{'item_id': 1, 'item': {'name': 'flow', 'price': 2.3}}
3. Summary
This article details the basic usage methods of query parameters in FastAPI, including URL splicing and required parameters, default values, optional parameters and the use of Pydantic models as query parameters. Through these methods, query parameters can be flexibly processed and passed, achieving richer functional requirements.
This is the article about getting the query parameters in FastAPI in one article. For more related FastAPI query parameters, please search for my previous article or continue browsing the related articles below. I hope everyone will support me in the future!