0

I am attemping to get data from an HTTP post using the following snippet:

import request

# Using @ to signify personal info
url = "@@@@@"

querystring = {"user":"@@@@@yahoo.com"}

headers = {
    'token': 1234,
    'content-type': "application/x-www-form-urlencoded",
    'host': "@@@",
    'connection': "Keep-Alive",
    'accept-encoding': "gzip",
    'content-length': "0",
    'cache-control': "no-cache"
}

response = requests.request("POST", url, headers=headers,     params=querystring)

print(response.text)

I get the following result (the ... is the rest of the 'data' string):

{'data': 'H4sIAAAAAAAAAO...T7R358EQT/u3/8D+8Rtv5/DwIA', 'error_code': 0, 'response_desc': 'SUCCESS'}

Unlike all other questions I have found on stackoverflow, the gzipped data of interest lives within a json response. The json object itself is not compressed. I have tried

zlib.decompress(response.json().get("data"),15 + 32)

But get the error

    TypeError: a bytes-like object is required, not'str'

Am I going about handling the decoding correctly? I see plenty of documentation on decoding a gz file but this is just a string that is gz compressed.


Additional attempt: trying

zlib.decompress(response.json().get("data").encode(), 16 + zlib.MAX_WBITS)

Gives me the error:

incorrect header checkk
8
  • 1
    Try zlib.decompress(response.json().get("data").encode(), 15 + 32) Commented Jun 8, 2017 at 20:46
  • Hmm... I get an error: "incorrect header check". I feel confident that the data is valid gz since I can use an online decoder such as txtwizard.net/compression to get the correct decoded string. Commented Jun 8, 2017 at 20:48
  • 1
    Yes. Try zlib.decompress(response.json().get("data").encode(), 16 + zlib.MAX_WBITS) The previous header was incorrect it seems. Commented Jun 8, 2017 at 20:51
  • Same "incorrect header check" :( Commented Jun 8, 2017 at 20:53
  • Is there a way you can share your compressed data? It would help with debugging. Commented Jun 8, 2017 at 20:58

2 Answers 2

4

This answer's credit is to the authors of http://www.txtwizard.net/compression:

It turns out the string needed to be base64 decoded first before being decompressed with the format suggested by @coldspeed in the comments.

zlib.decompress(base64.b64decode(response.json().get("data")), 16 + zlib.MAX_WBITS)
Sign up to request clarification or add additional context in comments.

Comments

0

You need to convert the string into a byte array first. This can be done two ways

bytes = bytes(your_string)

or

bytes = your_string.encode()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.