[英]I am calling a Rest API Curl command using python, But the post request is not working
So here is my curl command, In command line, curl script is working fine 这是我的curl命令,在命令行中,curl脚本运行正常
curl -X POST -H "Content-Type: application/json" -H "Accept: application/json" -u user:password URL -d '{"key1":"value1","key2":"value2"}'
Now I have converted my code to python and the code look like this : 现在,我将代码转换为python,代码如下所示:
import requests
import json
headers = {
'Content-Type': 'application/json',
'Accept': 'application/json',
}
data = '{"key1":"value1","key2":"value2"}'
requests.post('URL', headers=headers, data=data, auth=('user', 'password'))
When I ran this code, I am not getting any output. 运行此代码时,没有任何输出。 Please let me know what I am doing wrong in here.
请让我知道我在这里做错了什么。
At first, the 'url' parameter in requests.post
should be an complete url including the 'http://' or 'https://'. 首先,
requests.post
的“ url”参数应为完整的URL,包括“ http://”或“ https://”。
Secondly, if you need to post some data in form of application/json
, using Python protogenic dict
will be fine. 其次,如果您需要以
application/json
形式发布一些数据,那么使用Python的protogenic dict
就可以了。 like this 像这样
data = {"key1":"value1","key2":"value2"}
you might be looking at something like this? 您可能正在看这样的东西?
r = requests.post("http://{}/Command?".format(web_addr), params=dict)
print r.text
In Python 3.6.1, you should get the response 200 message if the following code gets executed, with a existing 'URL': 在Python 3.6.1中,如果执行以下代码并使用现有的“ URL”,则应该收到响应200消息:
from requests import post
data = {"key1":"value1","key2":"value2"}
print(post('URL', json=data))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.