unable to decode Python web request unable to decode Python web request python-3.x python-3.x

unable to decode Python web request


According to the response.headers (that you did not provide, but that are easily recoverable by running your code), the response is encoded using Brotli compression (Content-Encoding': 'br'). You can decompress it with brotlipy:

import brotlibrotli.decompress(response.content)#b'{"status":"success","data":[{"placed_by":"XE4670","order_id":"180331000000385",  #"exchange_order_id":null,"parent_order_id":null,"status":"REJECTED",#"status_message":"ADAPTER is down","order_timestamp":"2018-03-31 07:59:42", #"exchange_update_timestamp":null,...}

Now, it's JSON, as promised ('Content-Type': 'application/json').


If the server only returns brotli compressed response, response should be decompressed then it will be ready to use.

Fortunately, since v2.26.0 update, requests library supports Brotli compression if either the brotli or brotlicffi package is installed. So, if the response encoding is br, request library will automatically handle it and decompress it.

First;

pip install brotli,

then;

import requestsr = requests.get('some_url')r.json()


I fixed this issue by changing the request header,

headers = {    'accept-encoding': 'gzip, deflate, br',    ...}

to

headers = {    'accept-encoding': 'gzip, deflate, utf-8',    ...}