Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

315
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda