Dima Kashak
Dima Kashak

Reputation: 21

How to get information without using pyCURL (requests)

I need to implement the following curl:

curl -k https://somelinkhere/get_info -d auth_info='{"session_id":"blablablaidhere"}' -d params='{"id":"12345"}'

Currently I have the following code. It is working, but not exactly as I need. I need to get json content from the reply, just one parameter.

url = 'https://somelinkhere/get_info'
data = {'auth_info':'{"session_id":"blablablaidhere"}', 'params':'{"id":"12345"}'}
response = requests.post(url, data=data)
res = response.content
print res

Now it returns

'�Z[o�6�+���֖���ې�0�{h�`
                        AK�M�"����o�9�dI���t��@RI<��"�GD�D��.3MDeN��
                                                                           ��hͣw�fY)SW����`0�{��$���L��Zxvww����~�qA��(�u*#��݅Pɣ����Km���' 
etc. 

What i need is to output

res['balance_info']['balance']

If i launch cURL (provided above) from a command line, i have the following:

{"balance_info":{"enabled":"Y","balance":"0.55000","out_date_format":"MM-DD-YYYY","password":"12345","blocked":"N"

But do not know how to get this parameter using python script

Upvotes: 1

Views: 161

Answers (3)

Laurent LAPORTE
Laurent LAPORTE

Reputation: 22952

Here is an example using Requests:

>>> import requests
>>> r = requests.post('https://somelinkhere/get_info',
    data = {"id": "12345"})

See also the documentation to install Requests on your virtualenv.

Upvotes: 0

Banuta Alin Alexandru
Banuta Alin Alexandru

Reputation: 113

As in documentation,content property gives the binary version of response. You'll need to get the decoded version of request using .text then load it as json.

response = requests.post(url, data=data)
#load it as json
item = json.loads(response.text)

And now you can access your keys as:

response['balance_info']['balance']

Upvotes: 1

Setop
Setop

Reputation: 2490

What you get is a Gziped JSON string. You have to decompress before reading json. Then you can use the response as a python dict.

Something like res = json.loads(zlib.decompress(response.content))

Upvotes: 0

Related Questions