How do I get a callback function name #11450
Answered
by
hanzhenbjm
hanzhenbjm
asked this question in
Questions
-
First Check
Commit to Help
Example Codeclass LogMiddleware(BaseHTTPMiddleware):
"""log record test"""
def __init__(self, app):
super().__init__(app)
async def dispatch(self, request: Request, call_next) -> Response:
g.transaction_id = str(uuid.uuid4()).replace('-', '')
http_method = request.method
request_url = str(request.url)
request_content = str(request.query_params)
header = request.headers.get("user-agent")
start_time = time.time()
request_time = datetime.datetime.now()
body = await request.body()
print(f"Body: {body}")
response = await call_next(request)
process_time = time.time() - start_time
response_time = datetime.datetime.now()
body = b""
async for chunk in response.body_iterator:
body += chunk
result = json.loads(body.decode("utf-8"))
print(result)
return Response(
content=body,
status_code=response.status_code,
headers=dict(response.headers),
media_type=response.media_type
)
@router.get("/orders")
async def order_info(ok: str | None = None):
return {"message": "Hello Order: %s" % ok}
|
Beta Was this translation helpful? Give feedback.
Answered by
hanzhenbjm
Apr 17, 2024
Replies: 1 comment 3 replies
-
request.scope.get("route").name |
Beta Was this translation helpful? Give feedback.
3 replies
Answer selected by
Kludex
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
request.scope.get("route").name