Skip to content

Instantly share code, notes, and snippets.

Last active Jun 8, 2021
What would you like to do?
import sys
from fastapi import FastAPI, Request
from pydantic import BaseModel
from typing import List
from loguru import logger
from starlette.routing import Match
from fastapi import FastAPI, APIRouter, Depends
from starlette.types import Message
logger.add(sys.stdout, colorize=True,
format="<green>{time:HH:mm:ss}</green> | {level} | <level>{message}</level>")
app = FastAPI()
async def set_body(request: Request, body: bytes):
async def receive() -> Message:
return {"type": "http.request", "body": body}
request._receive = receive
async def get_body(request: Request) -> bytes:
body = await request.body()
await set_body(request, body)
return body
async def log_middle(request: Request, call_next):
logger.debug(f"{request.method} {request.url}")
routes =
for route in routes:
match, scope = route.matches(request)
if match == Match.FULL:
for name, value in scope["path_params"].items():
logger.debug(f"\t{name}: {value}")
logger.debug(await get_body(request))
response = await call_next(request)
return response
class User(BaseModel):
id: int
name: str = "Natsumi""/user/")
def create_user(user: User):
return {"res": "ok", "ID":, "名前":}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment