【问题标题】:I get different results when calling from Postman and from a Python script从 Postman 和 Python 脚本调用时,我得到不同的结果
【发布时间】:2021-04-27 09:37:33
【问题描述】:

我正在尝试调用网络服务。从 Postman 和 Python 脚本调用时,我得到不同的结果。

这里是python脚本:

url = 'https://api.dinero.dk/v1/257403/contacts'
payload="{\r\n'Name': 'Lars5',\r\n'Street': 'Præsteager 5',\r\n'ZipCode': '8200',\r\n'City': 'Aarhus N',\r\n'CountryKey': 'DK',\r\n'Phone': '+45 12 34 56 78',\r\n'Email': 'test1@test.com'\r\n}"
headers = {'Content-Type': 'application/json','Authorization': 'Bearer ' + token}
response = requests.request('POST', url, headers=headers, data=payload)

返回状态 500

如果我在 Postman 中做同样的事情:

然后我得到状态 201。

有什么区别?

【问题讨论】:

  • 对于初学者来说,邮递员中有 13 个标头,但 Python 请求中有 2 个标头
  • 您可以在 postman 中根据请求生成 Python 代码。它将使用urllib,但它是比 2 个严重裁剪的屏幕截图更好的起点
  • 对不起,我应该提到这一点。 python代码取自邮递员代码生成器。

标签: python json python-requests postman


【解决方案1】:

在 postman 中你的 json 格式错误,你应该使用双引号而不是 json 的单引号,json 中没有单引号,并且在 postman 中它正确突出显示语法错误。

您是否从原始正文下拉列表中选择了 json?

用双引号代替单引号来解决一半的问题

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-08-22
    • 2021-04-07
    • 2019-12-23
    • 1970-01-01
    • 2019-01-06
    • 2018-08-01
    • 2020-11-30
    • 2018-11-21
    相关资源
    最近更新 更多