使用 shell 时,我可以通过运行成功创建新用户
curl --user 管理员:密码“Content-Type:application/json” https://localhost:8080/midpoint/ws/rest/users -d @user.json
但是,当我尝试使用请求在 python 中执行相同的操作时,我收到 200 响应并且没有创建用户。
这是我正在使用的脚本:
import requests
headers = {
'Content-Type': 'application/json',
}
data = open('user.json')
response = requests.post('https://localhost:8080/midpoint/ws/rest/users', headers=headers, data=data, auth=('Administrator', 'password'))
print(response)
对我来说它们看起来是一样的。阻止创建用户的 python 请求有什么不同?
最佳答案
我将发布日期与 curl
和 python 请求
进行比较。我发现了差异。
CURL: {"user" : "hero","pd":30}
Requests: pd=30&user=hero
这就是我的测试。
import requests
import json
headers = {
'Content-Type': 'application/json',
}
with open('user.json') as j:
data = json.load(j)
response = requests.post('http://127.0.0.1:8080',
headers=headers,
json = data,
auth=('Administrator', 'password'))
print(response.headers)
关于Python-使用requests时post请求失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52546503/