Print raw HTTP request in Flask or WSGI

前端 未结 7 1221
名媛妹妹
名媛妹妹 2021-02-01 13:05

I am debugging a microcontroller I\'ve built which is writing raw HTTP requests line by line. I am using Flask for my backend and I would like to see the entire request as it a

7条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-01 13:31

    Why not?

    from flask import Flask, request
    
    app = Flask(__name__)
    
    @app.before_request
    def log_request():
        app.logger.debug("Request Headers %s", request.headers)
        return None
    
    # The remaining application code.
    

    I've used the headers but you can use the same aproach to print any request attribute. The docs are here: http://flask.pocoo.org/docs/0.12/api/#flask.Request.

    Also you need to setup FLASK_DEBUG=1 to Flask.logger.debug to work, what is nice since you can disable it in production.

    Regards,

提交回复
热议问题