flask-openapi3
flask-openapi3 copied to clipboard
How to get the parsed Pydantic models of an endpoint globally?
I want to access the body of a request in a error handler, but right now I'm having to re-parse the body which is very inneficient.
If this is not possible yet then I leave this as a suggestion
# Not ideal:
@api.error_handler(400)
def log_error_payload(e: HTTPException):
if flask.request.is_json:
payload_dict = json.loads(flask.request.get_json())
log_payload(payload_dict)
# Ideal:
@api.error_handler(400)
def log_error_payload(e: HTTPException):
if flask.request.openapi.body:
payload_dict = flask.request.openapi.body.dict()
log_payload(payload_dict)
flask-openapi3
don't provide an interface such as flask.request.openapi.body
.
But you can handle it by your self:
from flask import g, request
@app.post("/book")
def get_book(body: BookBody):
g.body= body
request.body= body
abort(400)
...
@app.errorhandler(400)
def log_error_payload(e):
print(e)
print(g.body)
print(request.body)
This issue has been automatically closed because we haven't heard back for more than 365 days, please reopen this issue if necessary.