【问题标题】:Python Post call throwing 400 Bad RequestPython Post调用抛出400 Bad Request
【发布时间】: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被添加

标签: python rest curl


【解决方案1】:

尝试使用requests(使用pip install requests 安装)而不是urllib

另外,请将您的数据以JSON 的形式附在请求正文中,不要将它们作为 URL 参数传递。您也在 curl 示例中传递 JSON 数据。

import requests


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/",
    json=data
)

print response.status_code, response.reason

编辑

来自https://2.python-requests.org/en/master/user/quickstart/#more-complicated-post-requests

注意,如果datafiles 被传递,json 参数将被忽略。

在请求中使用json参数会改变Content-Typeapplication/json 的标题中。

【讨论】:

  • 谢谢,我的错误是因为在请求中使用data 而不是json :-)
  • 与 requests.put 有同样的问题 - 错误地传递了 data = {},而 http 400 失败了 - 当我使用 json = {} 时它成功了。谢谢 setevoy,Dusan Madar
【解决方案2】:

您的代码中的问题是,您将 Content-Type 标头设置为 application/json 并且没有以 json 格式发送数据

 import httplib, json

 httpServ = httplib.HTTPConnection("127.0.0.1", 9100)
 httpServ.connect()
 headers = {"Content-type": "application/json"}
 data = json.dumps({
    "externalId": "801411",
    "name": "RD Core",
    "description": "Tenant create",
    "subscriptionType": "MINIMAL",
    "features": {
        "capture": False,
        "correspondence": True,
        "vault": False
    }
 })
 # here raw data is in json format
 httpServ.request("POST", "/tenants", data, headers)
 response = httpServ.getresponse()
 print response.status, response.reason
 httpServ.close()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-05-29
    • 2013-10-02
    • 2012-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多