flask-openapi3 icon indicating copy to clipboard operation
flask-openapi3 copied to clipboard

How to get the parsed Pydantic models of an endpoint globally?

Open mojimi opened this issue 1 year ago • 1 comments

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)

mojimi avatar Jun 20 '23 21:06 mojimi

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)

luolingchun avatar Jun 21 '23 01:06 luolingchun

This issue has been automatically closed because we haven't heard back for more than 365 days, please reopen this issue if necessary.

github-actions[bot] avatar Sep 01 '24 01:09 github-actions[bot]