Setup:
- connexion[swagger-ui]==2.13.0
- Flask==2.2.5
I am using connexion (Not Flask directly), to set up my app and host swagger. I want to print out every request and response payload to console before sending it.
I tried using these after building my connexion app
# Add pre payload printer to app
connex_app.app.before_request(print_request(config))
connex_app.app.after_request(print_response(config))
Here are those functions
def print_request(config):
'''
Print out requests received, or response sent
'''
def request_logger():
if request.method not in ('GET'):
if config.myproject.logging.myproject.pretty_print.request_payloads:
logger.debug(f"Request received, Body: {request.get_data(as_text=True)}")
else:
data = request.get_json() if request.is_json else {}
flat_json = json.dumps(data, separators=(',', ':'))
logger.debug(f"Request received, Body: {flat_json}")
else:
logger.debug("Request received")
return request_logger
def print_response(config):
'''
Print out requests received, or response sent
'''
def response_logger(response):
# Ensure you have access to the correct response object here, this might need to be passed explicitly
if config.myproject.logging.myproject.pretty_print.response_payloads:
logger.debug(f"Response sent, Body: {response.get_data(as_text=False)}")
else:
# This section might need adjustment based on how you're handling response data
data = response.get_json() if response.is_json else {}
flat_json = json.dumps(data, separators=(',', ':'))
logger.debug(f"Response sent, Body: {flat_json}")
return response
return response_logger
The problem is this line
logger.debug(f"Response sent, Body: {response.get_data(as_text=False)}")
For some reason it tries to redirect swagger-ui requests and responses to its interface, which then breaks the swager-ui.
This is very odd to me, as I did not have this issue yesterday. I did have connexion dependency set to 2.*, and I have no idea what version was being used before.
I do not have time to change all my code to use connexion 3.0, so that is not an option right now. Also I had to set Flask==2.2.5 because of a JSONEncoding problem with newer versions.
Has anyone else ran into this before? Am I printing responses properly? is there a better way to do it?
This just feels like a dependency issue going on and I am not sure how to fix it...
I gave up trying to get this to work without breaking swagger. I disabled using this method, and ended up creating my own decorator, and attached to every route path that I wanted to print the response for. Its not a great workaround but it meets my needs.