【问题标题】:Why might I be getting a 500 Error when using Python Requests, but not using cURL为什么我在使用 Python 请求时会收到 500 错误,但不使用 cURL
【发布时间】:2016-06-23 08:22:44
【问题描述】:

我正在尝试使用 python 的请求来实现 Synchroteam API 的基本实现。我能够使用 cURL 作为概念证明建立连接并获取我想要的所有数据,但是使用请求我可以使用我的凭据进入该站点,但我得到的只是:

500
text/html; charset=utf-8
https://apis.synchroteam.com/Api/v1/user/list

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>Error</title>
</head>
<body>
    <h2>
        An error occurred while processing your request.
    </h2>
</body>
</html>

Process finished with exit code 0

当在这一行中使用 curl 时,一切都会返回正确的信息:

curl -u domain:APIKEY -H "Accept: application/json" -H "Content-Type: application/json" https://apis.synchroteam.com/Api/v1/user/List

我的一小段python代码是这样的:

import requests


apiurl = 'https://apis.synchroteam.com/Api/v1/user/list'
login = ("domain", "APIKEY")
headers = {'Content-Type': 'application/json'}

def run():
    r = requests.get(apiurl, auth=login, headers=headers)

    print r.status_code
    print r.headers['Content-type']
    print r.url
    print r.text

run()

所有的 Synchroteam 文档都有 PHP 示例,但我看不出有什么理由不能使用 python。 文档在这里:http://api.synchroteam.com/rest.php

任何见解都会很棒,我真的希望它只是我缺少的一些小东西。 谢谢。

【问题讨论】:

  • 您是否也尝试过设置Accept: 标头?
  • 我没有。知道我应该将其设置为接受什么吗?我不确定它目前挂断了什么。很抱歉这个基本问题,这是我第一次使用任何 HTTP 或外部 API
  • 更新:我没有试过这个。我现在意识到这类似于 Curl 线。非常感谢,这已经困扰了我好几个小时了。

标签: php python curl python-requests


【解决方案1】:

这是我使用 Postman 生成的 Python 代码。我相信它会帮助你!

import requests

url = "https://apis.synchroteam.com/Api/v1/User/list"

payload = "{\n    \"status\":\"1\"\n}"
headers = {
'content-type': "application/json",
'accept': "application/json",
'authorization': "Basic ZGVtbzoxMjEyOThucDIwMDM=",
'cache-control': "no-cache",
'postman-token': "1a3839b6-db5b-0c07-be1a-2816f491476c"
}

response = requests.request("GET", url, data=payload, headers=headers)

print(response.text)

只需更改身份验证!
尼克

【讨论】:

  • 它没有给出关于这个问题的线索。
  • 感谢您的回复,我能够在 ig0774 的帮助下解决这个问题。我只需要 headers = {'Accept': 'application/json'} 就可以解决所有问题。以后我可能需要考虑做一些更高级的标题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-25
  • 2015-03-15
  • 1970-01-01
  • 1970-01-01
  • 2019-02-08
  • 2016-04-24
相关资源
最近更新 更多