Python Post调用400 Bad Request
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Python Post调用400 Bad Request,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1592字,纯文字阅读大概需要3分钟。
内容图文
![Python Post调用400 Bad Request](/upload/InfoBanner/zyjiaocheng/763/af307863a8b44617b3fc2d582c64988d.jpg)
我正在编写一个python脚本,它将调用一个REST POST端点,但作为响应,我收到400 Bad Request,好像我用curl做同样的请求,它返回200 OK.下面是python脚本的代码片段
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 -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脚本中的问题在哪里.
解决方法:
尝试使用请求(使用pip安装请求安装)而不是urllib.
另外,将您的数据作为JSON包含在请求正文中,不要将它们作为URL参数传递.您也在curl示例中传递JSON数据.
import requests
headers = {
"content-type": "application/json"
}
data = {
"externalId": "801411",
"name": "RD Core",
"description": "Tenant create",
"subscriptionType": "MINIMAL",
"features": {
"capture": False,
"correspondence": True,
"vault": False
}
}
response = requests.post(
url="http://localhost:9100/tenants/",
headers=headers,
json=data
)
print response.status_code, response.reason
内容总结
以上是互联网集市为您收集整理的Python Post调用400 Bad Request全部内容,希望文章能够帮你解决Python Post调用400 Bad Request所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。