[Python] Picking up the body post request from fastAPI server

Discussão em 'Python' iniciado por Stack, Dezembro 8, 2024.

  1. Stack

    Stack Membro Participativo

    I wonder how to pick up the body post request from a fast api server in order to transform this body to making a new body to post another server.

    But it does not seem like in express javascript when we could reach the body by using the expression req.body.

    I have searched in the documentation but I have not found any example.

    Is there any equivalent for this in python fast api ?

    EDIT :

    here is my current code. After some researchs, it seems that we have to create some class to parse the body but I am not sure...

    import json
    from random import random

    from fastapi import FastAPI
    from pydantic import BaseModel

    app = FastAPI()


    class Body(BaseModel):
    title: str

    @app.post("/posts/")
    async def posts(body: Body):
    number=random()
    title = body
    posts[number]={
    "msgtype":"PostCreated",
    "data":{
    "id": number,
    "title": title,
    }
    }
    return posts[number]

    Continue reading...

Compartilhe esta Página