| 1 |
from fastapi import FastAPI, Request |
| 2 |
|
| 3 |
app = FastAPI() |
| 4 |
|
| 5 |
|
| 6 |
def magic_data_reader(raw_body: bytes): |
| 7 |
return { |
| 8 |
"size": len(raw_body), |
| 9 |
"content": { |
| 10 |
"name": "Maaaagic", |
| 11 |
"price": 42, |
| 12 |
"description": "Just kiddin', no magic here. ✨", |
| 13 |
}, |
| 14 |
} |
| 15 |
|
| 16 |
|
| 17 |
@app.post( |
| 18 |
"/items/", |
| 19 |
openapi_extra={ |
| 20 |
"requestBody": { |
| 21 |
"content": { |
| 22 |
"application/json": { |
| 23 |
"schema": { |
| 24 |
"required": ["name", "price"], |
| 25 |
"type": "object", |
| 26 |
"properties": { |
| 27 |
"name": {"type": "string"}, |
| 28 |
"price": {"type": "number"}, |
| 29 |
"description": {"type": "string"}, |
| 30 |
}, |
| 31 |
} |
| 32 |
} |
| 33 |
}, |
| 34 |
"required": True, |
| 35 |
}, |
| 36 |
}, |
| 37 |
) |
| 38 |
async def create_item(request: Request): |
| 39 |
raw_body = await request.body() |
| 40 |
data = magic_data_reader(raw_body) |
| 41 |
return data |