POST call with only one numeric parameter in FastAPI
The error basically says that, the required query parameter "process_id" is missing. The reason is that you send a POST request with request body (payload) i.e., JSON data; however, your endpoint expects a query parameter. To receive the data in JSON format, one needs to create a Pydantic BaseModel as below, and send the data from the client in the same way you already do.
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI()
class Item(BaseModel):
process_id: int
@app.post("/do_something/")
async def do_something(item: Item):
# some code
return item
If you, however, need to pass a query parameter, then you create an endpoint in the same way you did, but in the client you add the parameter to the URL itself, as shown below:
def test_do_something():
response = client.post("/do_something/?process_id=16")
return response.json()
UPDATE
Alternatively, you can pass a single body parameter using Body(..., embed=True), as shown below:
@app.post("/do_something/")
async def do_something(process_id: int = Body(..., embed=True)):
return process_id