I am creating a fast api application. basic sample is below. I added a another route and method named test and want to access the request sent by clients in post request. I have looked at pydantic to create model classes to map incoming request to model classes. but i want to know , where/how does the mapping happen.
say my client posts a request via javascript, as such .
to read this request, what do i need to configure. how can i map this request , to a model class and then to my route method.
also, i want to verify few things that are passed in the request header in my route method. how do i access both headers and the body (mapped to my model class) . does fastapi with pydantic , maps the request object to our model class or do i have to write that code too?
fetch("https://myapi.example.com/text", {
method: "POST",
body: JSON.stringify({
userId: 1,
title: "whatever",
completed: false
}),
headers: {
"Content-type": "application/json; charset=UTF-8"
}
});
app.py
from fastapi import FastAPI, Request
app = FastAPI()
@app.get("/")
async def root():
return {"message": "Hello, World!"}
@app.POST("/test")
async def Test(request: Request):
...