Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

314
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda