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
Full Debug / Dump of a Request #331
Comments
This wouldn't be too hard to implement. IMO we should look to requests-toolbelt for inspiration on how some functionality like multipart form encoding to work like that by default. Toolbelt is basically Requests+ Not sure about this exact feature but some of them might be good for us. |
Agreed. I too wanted that functionality and so copy-pasted toolbelt's version and tweaked it for httpx, making it much less configurable in the process: # Inspired by https://github.com/requests/toolbelt/blob/0.9.1/requests_toolbelt/utils/dump.py
def _log_response(response: httpx.models.BaseResponse) -> str:
req_prefix = "< "
res_prefix = "> "
request = response.request
output = []
output.append(f"{req_prefix}{request.method} {request.url} {response.protocol}")
for name, value in request.headers.items():
output.append(f"{req_prefix}{name}: {value}")
output.append(req_prefix)
if isinstance(request.content, (str, bytes)):
output.append(f"{req_prefix}{request.content}")
else:
output.append("<< Request body is not a string-like type >>")
output.append("")
output.append(
f"{res_prefix}{response.protocol} {response.status_code} {response.reason_phrase}"
)
for name, value in response.headers.items():
output.append(f"{res_prefix}{name}: {value}")
output.append(res_prefix)
output.append(f"{res_prefix}{response.text}")
return "\n".join(output)
def dump_all_requests(response: httpx.models.BaseResponse) -> str:
"""Dump all requests and responses including redirects.
"""
data = []
history = list(response.history[:])
history.append(response)
for response in history:
data.append(_log_response(response))
return "\n".join(data) This could also be done with a LoggingDispatcher, and I would be my preferred method I think, but I haven't actually implemented that just yet. Take this as inspiration, but not a definitive answer. |
You could probably use the newly added middleware support :D |
Do we allow wrapping other middleware from the outside already? Don’t think we do, and that might be a nice next step as well as documenting things. :) |
Ah right, sorry! I'm just too excited about custom middleware. :) |
Yeah third party middleware will be the right tack for this. |
Closing this as out-of-scope to the core package. |
Is there any functionality that replicates dump from requests_toolbelt.utils , that dumps out full headers and content etc from the response so you can debug your requests... as I cannot find any reference to debugging in the documentation...
IE in requests
Would result in..
Cheers
The text was updated successfully, but these errors were encountered: