Accessing META data and others parameters in http request

Hi Everyone,

I have a Basic service to which I have access through REST channel

from zato.server.service import Service


class GetUserDetails(Service):
    """ Returns details of a user by the person's ID.

    This is a test to check if it works
    """
    name = 'api.user.get-details'

    class SimpleIO:
        input_required = ('user_name', 'email')

    def on_completed(self, result: CommandResult) -> None:
        # This will run when the command has finished
        self.logger.info('Received result: %s', result)

    def handle(self):
        # For now, return static data only
        self.response.payload = {
            'id': "111",
            'user_name': 'John Doe',
            'user_type': 'SRT'
        }

        self.logger.info('http information %s' % self.request.http)

I would like to log HTTP_X_FORWARDED_FOR and REMOTE_ADDR. I have logged self.request.http and I can see information there, but I don’t know how to access them

zato38   | 2022-05-13 19:58:30,997 - INFO - 3065:Dummy-545 - api.user.get-details:0 - TEST <HTTPRequestData at 0x7fc4c0dd6ee0; GET:Bunch(); POST:Bunch(); _wsgi_environ:{'wsgi.version': (1, 0), 'wsgi.multithread': True, 'wsgi.multiprocess': False, 'wsgi.run_once': False, 'wsgi.file_wrapper': <class 'zato.server.ext.zunicorn.http.wsgi.FileWrapper'>, 'SERVER_SOFTWARE': 'Zato', 'wsgi.input': <zato.server.ext.zunicorn.http.body.Body object at 0x7fc4bdd23dc0>, 'gunicorn.socket': <gevent._socket3.socket at 0x7fc4c1625700 object, fd=51, family=2, type=1, proto=6>, 'REQUEST_METHOD': 'POST', 'QUERY_STRING': '', 'RAW_URI': '/test', 'SERVER_PROTOCOL': 'HTTP/1.1', 'CONTENT_TYPE': 'application/json', 'HTTP_USER_AGENT': 'PostmanRuntime/7.29.0', 'HTTP_ACCEPT': '*/*', 'HTTP_POSTMAN_TOKEN': '31cea10a-9f1c-4efb-9b9a-9957fcd72af8', 'HTTP_HOST': 'localhost:8083', 'HTTP_ACCEPT_ENCODING': 'gzip, deflate, br', 'CONTENT_LENGTH': '63', 'HTTP_X_FORWARDED_FOR': '172.18.0.1', 'HTTP_CONNECTION': 'close', 'wsgi.url_scheme': 'http', 'REMOTE_ADDR': '127.0.0.1', 'REMOTE_PORT': '51978', 'SERVER_NAME': '0.0.0.0', 'SERVER_PORT': '17010', 'PATH_INFO': '/test', 'SCRIPT_NAME': '', 'zato.local_tz': <StaticTzInfo 'Etc/UTC'>, 'zato.request_timestamp_utc': datetime.datetime(2022, 5, 13, 19, 58, 30, 996313), 'zato.request_timestamp': datetime.datetime(2022, 5, 13, 19, 58, 30, 996313, tzinfo=<StaticTzInfo 'Etc/UTC'>), 'zato.http.response.headers': {'X-Zato-CID': '318bf12f07ad1a562d6f38e4'}, 'zato.http.remote_addr': '172.18.0.1', 'zato.channel_item': {'connection': 'channel', 'content_type': None, 'data_format': 'json', 'host': None, 'id': 85, 'has_rbac': False, 'impl_name': 'TestFirstService.GetUserDetails', 'is_active': True, 'is_internal': False, 'merge_url_params_req': True, 'method': 'POST', 'name': 'test', 'params_pri': 'channel-params-over-msg', 'ping_method': None, 'pool_size': None, 'service_id': 585, 'service_name': 'api.user.get-details', 'soap_action': '', 'soap_version': None, 'transport': 'plain_http', 'url_params_pri': 'qs-over-path', 'url_path': '/test', 'sec_use_rbac': False, 'cache_type': None, 'cache_id': '', 'cache_name': None, 'cache_expiry': 0, 'content_encoding': '', 'match_slash': True, 'hl7_version': 'hl7-v2', 'json_path': '', 'should_parse_on_input': False, 'should_validate': False, 'should_return_errors': False, 'data_encoding': 'utf-8', 'is_audit_log_sent_active': False, 'is_audit_log_received_active': False, 'max_len_messages_sent': 50, 'max_len_messages_received': 50, 'max_bytes_per_message_sent': 500, 'max_bytes_per_message_received': 500, 'service_whitelist': '', 'service_impl_name': 'TestFirstService.GetUserDetails', 'match_target': ':::POST:::haanyHTTP_SEPhaany:::/test', 'match_target_compiled': <zato.url_dispatcher.Matcher object at 0x7fc4bdd7cba0>, 'is_rate_limit_active': False, 'rate_limit_def': '', 'rate_limit_type': 'APPROXIMATE', 'rate_limit_check_parent_def': False}, 'zato.http.raw_request': b'{\n    "user_name": "user_name",\n    "email": "steve@mail.com"\n}', 'zato.oauth.post_data': {}, 'zato.http.GET': {}, 'zato.http.POST': {}, 'zato.http.path_params': {}}; method:'POST'; params:Bunch(); path:'/test'; user_agent:'PostmanRuntime/7.29.0'>

If you could help me about this case, it would be great.
Thank you guys

Hello,

details of both request and response objects are provided in the documentation here.

But, at this stage of your getting acquainted with Zato, what you also need is to start to use a debugger to be able to see live, with breakpoints, what is going on inside your services.

How to configure a debugger is documented here.

That is exactly what I need. Thank you.
Do you have the setting for Pycharm IDE, please ? Or is it the same with Visual studio Code?

Here are links to chapters in the documentation to do with PyCharm. You need to scroll down the page at the second one.

However, if you do not use VS Code then you are really doing yourself a disservice - nowadays, there is simply no comparison between VS Code and any other IDE, the former is years ahead of the rest.

Certainly, as far Zato is concerned, the focus will be on VS Code with some coverage for other products and I encourage you to take your programming to the next level with VS Code already.

1 Like

I see. I will do like you suggest and use VS Code.

Thank you very much