![](/img/trans.png)
[英]Why am I getting 500 Internal Server Error when calling post rest api via python request?
[英]I am calling a Rest API Curl command using python, But the post request is not working
这是我的curl命令,在命令行中,curl脚本运行正常
curl -X POST -H "Content-Type: application/json" -H "Accept: application/json" -u user:password URL -d '{"key1":"value1","key2":"value2"}'
现在,我将代码转换为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'))
运行此代码时,没有任何输出。 请让我知道我在这里做错了什么。
首先, requests.post
的“ url”参数应为完整的URL,包括“ http://”或“ https://”。
其次,如果您需要以application/json
形式发布一些数据,那么使用Python的protogenic dict
就可以了。 像这样
data = {"key1":"value1","key2":"value2"}
您可能正在看这样的东西?
r = requests.post("http://{}/Command?".format(web_addr), params=dict)
print r.text
在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.