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

871
Vistas
Using FastAPI in a sync way, how can I get the raw body of a POST request

Using FastAPI in a sync, not async mode, I would like to be able to receive the RAW, unchanged body of a post request.

All examples I can find show async code, when I try it in a normal sync way, the request.body() shows uo as a coroutine object.

And when I test it by posting some XML to this endpoint, I get a 500 "Internal Server Error"

from fastapi import FastAPI, Response, Request, Body

app = FastAPI()


@app.get("/")
def read_root():
    return {"Hello": "World"}

@app.post("/input")
def input_request(request: Request):

    # how can I access the RAW request body here?  
    body = request.body()

    # do stuff with the body here  

    return Response(content=body, media_type="application/xml")

Is this not possible with FastAPI?

Note: a simplified input request would look like

POST http://127.0.0.1:1083/input
Content-Type: application/xml

<XML>
    <BODY>TEST</BODY>
</XML>

and I have no control over how input requests are sent, because I need to replace an existing SOAP API

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

If an object is co-routine, it needs to be awaited. FastAPI is based on Starlette, and Starlette methods for returning the body of the request are async methods; thus, you need to "await" them.

UPDATE

Alternatively, if you are confident that the incoming data is a valid JSON, you can use the Body field, as below:

@app.post("/input")
def input_request(payload: dict = Body(...)):
    return payload

If, however, the incoming data is in XML format, as in the example you provided, it might be best to pass them via files instead (as below), using a tempfile module in your client.

@app.post("/input") 
def input_request(file: bytes = File(...)): 
    return file
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