Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Access to HTTP request? #1397

Open
silentbugs opened this issue Nov 3, 2023 · 1 comment
Open

Access to HTTP request? #1397

silentbugs opened this issue Nov 3, 2023 · 1 comment

Comments

@silentbugs
Copy link

Is it possible to directly access the request (python requests) object for low level HTTP handling?

I am writing a logger plugin and wanted to also log raw HTTP-related things, such as the method, request headers, request body, response headers, response body.

@atlantis-dsf
Copy link

You can generally turn on logging by the requests module like this

import logging
from http.client import HTTPConnection

def debug_requests_on():
    """Switches on logging of the requests module."""
    HTTPConnection.debuglevel = 1

    logging.basicConfig()
    logging.getLogger().setLevel(logging.DEBUG)
    requests_log = logging.getLogger("requests.packages.urllib3")
    requests_log.setLevel(logging.DEBUG)
    requests_log.propagate = True
    
debug_requests_on()

Sadly I don't have the link from where I got it, but it's proven very useful

Another way you can do this is by creating request/response hooks for a session and using that session as transport

from zeep.client import Client
from zeep.transports import Transport
from requests import Session, Response

def log_traffic(r: Response):
    print(r.headers, r.request.body, "etc...")

with Session() as s:
    s.hooks["response"].append(lambda r, *args, **kwargs: log_traffic(r))
    transport = Transport(session=s)
    c = Client(transport=transport)
    response = c.service.myoperation()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants