Reputation: 409
I have following a Apache server where brotli compression is enabled. When I send the curl request from my cli it works fine. However when i send the request from python it returns plain text without brotli compression. which means it did not use brotli compression.
Curl command that works:
curl -H "Accept-Encoding: br" http://testsite/index.html
Here is the python code:
headers = {
' Accept-Encoding': 'br',
}
response = requests.get('http://testsite/index.html', headers=headers)
How to make a request from python so that it will make a request to the server to use brotli compression.
Upvotes: 2
Views: 3926
Reputation: 134
Your sample does not show how you read the response. But you are using requests
relying on urllib3
, requesting response content to be decoded first based on content-encoding
header.
You can see the parameter in urllib3 documentation.
As brotli decoding is supported by urllib3, then it actually make sense that you receive the decoded content (considering brotli module is installed).
Is your actual request that you want to access the brotli encoded content, if so you will have to check requests documentation to see if there is a way to access the raw non decoded content.
Upvotes: 1
Reputation: 21
This have support for brotli requests https://github.com/encode/httpx.
Example from unit tests:
import brotli
import httpx
body = b"test 123"
compressed_body = brotli.compress(body)
headers = [(b"Content-Encoding", b"br")]
response = httpx.Response(200, headers=headers, content=compressed_body)
Upvotes: 2