-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
Retrieving raw response (status + headers + body) data from ClientResponse? #3877
Comments
Closing for now -- will just write a function to put together the original raw response and re-open this for opinion. |
The following works: async with aiohttp.ClientSession() as session:
async with session.get(url) as response:
status = response.status
reason = response.reason
headers = response.headers
response = await response.read()
resp = f"HTTP/1.1 {status} {reason}\r\n".encode("latin-1")
for header in headers:
resp += f"{header}: {headers[header]}\r\n".encode("latin-1")
resp += b"\r\n" + response However, I do believe a |
@synchronizing please reopen this issue. I'm desperate. |
Re-opening so devs can take a look in the future. In the meantime, I wound up creating my own module, httpsuite to take care of manipulating raw http requests. It might be of use to you (?). from httpsuite import Request
import json
request = Request(
method="GET",
target="/",
protocol="HTTP/1.1",
headers={"Host": "www.google.com", "Connection": "keep-alive",},
body=json.dumps({"hello": "world"}),
) I was thinking of adding a conversion methods for the |
Never mind lol. I already found a workaround for my problem. But, don't close this issue just leave it open. Nice library btw. |
@synchronizing wow, nice lib! I may end up using it for the HTTP test suite in CherryPy... By the way, have you checked if https://github.com/python-hyper/h11 has the APIs you need? |
As for exposing |
Oh man, I spent hours looking for a (as my Google query went) "manipulate raw HTTP python module," and at the time that I needed it I couldn't find anything.
Makes complete sense, and an edge case I never thought of. The original purpose for my question (not to fall into the XY problem, and in case you are curious) was for the creation of a man-in-the-middle service for auto-proxy rotation. I wrote a preliminary version of an mitm, and a shitty fix for an existing project that serves as a public proxy brokerage called ProxyBroker. Long story short, ProxyBroker does not support HTTPS and uses the request/responses between the users/destination services to check HTTP status codes (to see if proxy replied accordingly). At the time, in 2009, I wrote httpsuite and mitm to try to mitigate both of these issues. If you have any ideas how one might go about it with a bit more swagger it would be a helpful suggestion from an online stranger. |
I was wondering if there is a way to effectively get the entire raw response from a
ClientResponse
class. Take the following code for example:What I am trying to effectively do is get the complete and raw return from the server -- the status, header, and the response in one. I have checked out the docs, and I see there is no mention for raw complete data, only portions; status, header, and body.
Now, I did think of simply reformatting
status
andheader
, and then adding them to theresp
, but this seems a bit backward as I would be doing something thataiohttp
certainly does the opposite of in the background -- which is why I am coming here asking for guidance. Something like so would be a lifesaver:Thank you in advance, and any guidance would be appreciated!
The text was updated successfully, but these errors were encountered: