简体   繁体   English

将POST请求卷曲到python代码中

[英]Curl POST request into python code

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. 我需要从回复中获取json内容,只是一个参数。

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'] 我需要的是输出res ['balance_info'] ['balance']

Generally, -d would become data=... in requests. 通常,-d在请求中将变为data=... You curl data includes the auth_info and params as things sent to the server and perhaps would be better understood if you quoted the whole thing, eg -d 'auth_info={"session_id":"blablablaidhere"}' curl数据时将auth_infoparams作为发送到服务器的内容包括auth_info并且如果引用整个内容,则可能会更好地理解,例如-d 'auth_info={"session_id":"blablablaidhere"}'

Translating that into python: 将其翻译成python:

data = {'auth_info':'{"session_id":"blablablaidhere"}',
        'params':'{"id":"12345"}'})
response = requests.post(url, data=data)

Which would yield: 这将产生:

send: b'POST /get_info HTTP/1.1\r\nHost: somelinkhere\r\nConnection: keep-alive\r\nAccept-Encoding: gzip, deflate\r\nUser-Agent: python-requests/2.12.3\r\nAccept: */*\r\nContent-Length: 92\r\nContent-Type: application/x-www-form-urlencoded\r\n\r\n'
send: b'auth_info=%7B%22session_id%22%3A%22blablablaidhere%22%7D&params=%7B%22id%22%3A%2212345%22%7D'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM