【发布时间】:2023-03-21 22:45:02
【问题描述】:
我正在编写一个 python 脚本,它将调用一个 REST POST 端点,但作为响应,我收到 400 Bad Request,就像我使用 curl 执行相同的请求一样,它返回 200 OK。 python脚本的代码sn-p如下
import httplib,urllib
def printText(txt):
lines = txt.split('\n')
for line in lines:
print line.strip()
httpServ = httplib.HTTPConnection("127.0.0.1", 9100)
httpServ.connect()
params = urllib.urlencode({"externalId": "801411","name": "RD Core","description": "Tenant create","subscriptionType": "MINIMAL","features": {"capture":False,"correspondence": True,"vault": False}})
headers = {"Content-type": "application/json"}
httpServ.request("POST", "/tenants", params, headers)
response = httpServ.getresponse()
print response.status, response.reason
httpServ.close()
对应的curl请求是
curl -iX POST \
-H 'Content-Type: application/json' \
-d '
{
"externalId": "801411",
"name": "RD Core seed data test",
"description": "Tenant for Core team seed data testing",
"subscriptionType": "MINIMAL",
"features": {
"capture": false,
"correspondence": true,
"vault": false
}
}' http://localhost:9100/tenants/
现在我无法弄清楚 python 脚本的问题所在。
【问题讨论】:
-
请不要在您的问题中添加图片。请复制并粘贴文本,而不是文本图像,并将其粘贴到您的问题中。
-
我建议将您的代码复制并粘贴到帖子中,而不是使用屏幕截图。这将使人们能够更快地帮助您,因为他们可以测试您的代码。
-
屏幕截图被删除,python代码sn-p被添加