简体   繁体   English

如何将curl POST转换为Python中的POST请求?

[英]How to translate curl POST to requests POST in Python?

I have curl command which I need to transfer to requests POST in Python 我有curl命令,我需要转移到Python请求POST

curl -X POST -H "Content-Type: application/json" -d '{"val1": -1000, "val2": 15, "val3": "20000", "val4": "22"}'
http://some-site:6000?t=1234567890

I tried with: 我尝试过:

payload = {'val1': val1, 'val2': val2, 'val3': val3, 'val4': val4}
r = requests.post(url, params=payload)

r.url returns: r.url返回:

 http://some-site:6000?t=1234567890&val1=-1000&val2=15&val3=20000&val4=22

However, I get val1 is not defined response from server. 但是,我得到的val1 is not defined来自服务器的响应。 I can see in curl command that val1 and val2 are int type. 我可以在curl命令中看到val1和val2是int类型。 Can I send with requests ints and strings separately or this is not the reason for this error? 我可以单独发送requests和字符串,或者这不是导致此错误的原因吗?

Did you try requests.post(url, json=payload) ?.. params=payload append query string arguments and json=payload sends JSON as the body + content-type header 您是否尝试过requests.post(url, json=payload) ?.. params=payload附加查询字符串参数和json=payload发送JSON作为正文+内容类型标题

import requests
payload = {'val1': 1, 'val2': 2, 'val3': 3, 'val4': 3}
r = requests.post('http://httpbin.org/post', json=payload)
print(r.json())

>>> {u'args': {},
>>>  u'data': u'{"val3": 3, "val2": 2, "val1": 1, "val4": 3}',
>>>  ...

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

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