Created
April 3, 2024 16:55
-
-
Save Kyu/122b2ca4aa7577e3161a052dc9f7bd09 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask import Flask, request | |
import pprint | |
class LoggingMiddleware: | |
def __init__(self, _app): | |
self._app = _app | |
def __call__(self, env, resp): | |
error_log = env['wsgi.errors'] | |
pprint.pprint(('REQUEST', env), stream=error_log) | |
def log_response(status, headers, *args): | |
pprint.pprint(('RESPONSE', status, headers), stream=error_log) | |
return resp(status, headers, *args) | |
return self._app(env, log_response) | |
app = Flask(__name__) | |
# app.wsgi_app = LoggingMiddleware(app.wsgi_app) | |
@app.before_request | |
def log_request_info(): | |
pprint.pprint(('HEADERS', request.headers, | |
'BODY', request.get_data())) | |
app.run() | |
""" Testing | |
curl --header "Content-Type: application/json" \ | |
--request POST \ | |
--data '{"username":"xyz","password":"xyz"}' \ | |
http://localhost:5000 | |
""" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment