Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

312
Views
POST call with only one numeric parameter in FastAPI

I have a file called main.py in which I put a POST call with only one input parameter (integer), whose simplified code is:

from fastapi import FastAPI

app = FastAPI()

@app.post("/do_something/")
async def do_something(process_id: int):
    # some code
    return {"process_id": process_id}

Now, if I run the code for the test, saved in the file test_main.py, that is:

from fastapi.testclient import TestClient
from main import app

client = TestClient(app)

def test_do_something():
    response = client.post(
        "/do_something/",
        json={
            "process_id": 16
        }
    )
    return response.json()

print(test_do_something())

I get

{'detail': [{'loc': ['query', 'process_id'], 'msg': 'field required', 'type': 'value_error.missing'}]}

I can't figure out what the mistake is. It is necessary that it remains a POST call.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!