Httpx: Question: Enforce HTTP version for particular request

Created on 11 Sep 2019  Â·  4Comments  Â·  Source: encode/httpx

Hey, thanks for sharing this great library with the community.

Is there a documented way of enforcing particular HTTP protocol version per request?
Say I want to make http/1.1 request to the server and make http/2 request to the same server and validate if the response headers are matching my rules.

Thanks

Most helpful comment

Hi @paulchubatyy 👋

Following up on @sethmlarson's hint, we actually have an (apparently not documented yet? 👀) http_versions parameter on the Client/AsyncClient, which you can use to perform HTTP/1.1-only or HTTP/2-only requests, e.g.:

h11_client = httpx.Client(http_versions=["HTTP/1.1"])
h11_response = h11_client.get("https://myserver.com")

h2_client = httpx.Client(http_versions=["HTTP/2"])
h2_response = h2_client.get("https://myserver.com")

# Compare h11_response and h2_response…

I believe @tomchristie left out the per-request HTTP configuration in https://github.com/encode/httpx/pull/241 as it didn't seem to be a generally useful feature to have.

All 4 comments

That functionality isn't available currently, it feels like a pretty narrow use-case. The best workaround is probably to have two clients one configured for each HTTP version.

Hi @paulchubatyy 👋

Following up on @sethmlarson's hint, we actually have an (apparently not documented yet? 👀) http_versions parameter on the Client/AsyncClient, which you can use to perform HTTP/1.1-only or HTTP/2-only requests, e.g.:

h11_client = httpx.Client(http_versions=["HTTP/1.1"])
h11_response = h11_client.get("https://myserver.com")

h2_client = httpx.Client(http_versions=["HTTP/2"])
h2_response = h2_client.get("https://myserver.com")

# Compare h11_response and h2_response…

I believe @tomchristie left out the per-request HTTP configuration in https://github.com/encode/httpx/pull/241 as it didn't seem to be a generally useful feature to have.

@sethmlarson
I got carried away. Indeed setting the HTTP protocol version per request is overkill.

@florimondmanca
Thanks for the example, I'll try it out. Essentially this is exactly what I was asking for.

Sounds good, I’ll close this for housekeeping, feel free to report back. :)

Edit: went too fast — the PR you submitted is a nice addition. 🎉

Was this page helpful?
0 / 5 - 0 ratings