1

I'm using fastapi and python. Using a POST method to successfully return data. However, I need to return the request body of my post method to use for some business logic ideally before sending off the query to my database.

How do I go about doing this?

Currently trying return Request.json but it's returning a hexedecmial value.

kdrumz
  • 61
  • 11
  • Can you provide the code for the endpoint you currently have? – noninertialframe Oct 13 '21 at 19:23
  • "before sending off the query to my database" - do you want to return a result to the client before communicating with your database after the response has been sent? – MatsLindh Oct 13 '21 at 20:03
  • If I understand it correctly, you want to return response to the client in POST method before committing it to DB? – 3251_ali Oct 13 '21 at 20:26

1 Answers1

0

As mentioned here FastAPI: how to read body as any valid json?, you simply use the Request object:

from fastapi import Request, FastAPI

@app.post("/dummypath")
async def get_body(request: Request):
    return await request.json()

It return a python dictionary, if the request passed a json formatted body, otherwise check what you request is sending.

Fucio
  • 475
  • 3
  • 11