1

How can I use httpx library to do something similar to

curl --compressed "http://example.com"

Meaning I want the library to check if server supports compression, if so send the right header and return the data to me as if it was not compressed?

Ehud Lev
  • 2,461
  • 26
  • 38

2 Answers2

0

Try setting the content-encoding to gzip


import httpx

url = 'http://example.com'

headers = {
    'Accept-Encoding': 'gzip',
}

with httpx.Client() as client:
    response = client.get(url, headers=headers)
    content = response.content
ProfDFrancis
  • 8,816
  • 1
  • 17
  • 26
0

So apparently my question is incorrect, seems that the default will be using gzip.

I was running the following example to prove it:

import httpx

url = "http://example.com"

headers = {
    # "Accept-Encoding": "gzip, deflate",
}

response = httpx.get(url, headers=headers)

print(response.headers.get('content-encoding', "n/a"))
print(response.headers.get('content-length', "n/a"))

headers = {
     "Accept-Encoding": "identity",
}

response = httpx.get(url, headers=headers)
print(response.headers.get('content-encoding', "n/a"))
print(response.headers.get('content-length', "n/a"))

headers = {
     "Accept-Encoding": "deflate",
}

response = httpx.get(url, headers=headers)
print(response.headers.get('content-encoding', "n/a"))
print(response.headers.get('content-length', "n/a"))

OUTPUT:

gzip
648
n/a
1256
deflate
630

So from that I conclude that I don't need to do anything, I get it by default, I should only put the header if I want explicitly something else

Ehud Lev
  • 2,461
  • 26
  • 38