1 # ============LICENSE_START===============================================
2 # Copyright (C) 2022 Nordix Foundation. All rights reserved.
3 # ========================================================================
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 # ============LICENSE_END=================================================
18 from var_declaration import app
19 from flask import Flask, request, Response
22 TEXT_PLAIN='text/plain'
27 #Function to activate/deactivate http header and payload logging
28 @app.route('/payload_logging/<state>', methods=['POST', 'PUT'])
29 def set_payload_logging(state):
33 elif (state == "off"):
36 return Response("Unknown state: "+state+" - use 'on' or 'off'", 400, mimetype=TEXT_PLAIN)
38 return Response("Payload and header logging set to: "+state, 200, mimetype=TEXT_PLAIN)
40 # Generic function to log http header and payload - called before the request
41 @app.app.before_request
42 def log_request_info():
43 if (payload_log is True):
45 print('-----Request-----')
46 print('Req Headers: ', request.headers)
47 print('Req Body: ', request.get_data())
49 # Generic function to log http header and payload - called after the response
50 @app.app.after_request
51 def log_response_info(response):
52 if (payload_log is True):
53 print('-----Response-----')
54 print('Resp Headers: ', response.headers)
55 print('Resp Body: ', response.get_data())
58 # Helper function to check loggin state
59 def is_payload_logging():