我对 Python 比较陌生,所以需要一些帮助,我创建了一个脚本,它只使用请求库和基本身份验证来连接到 API 并返回 xml 或 Json 结果。
# Imports
import requests
from requests.auth import HTTPBasicAuth
# Set variables
url = "api"
apiuser = 'test'
apipass = 'testpass'
# CALL API
r = requests.get(url, auth=HTTPBasicAuth(apiuser, apipass))
# Print Statuscode
print(r.status_code)
# Print XML
xmlString = str(r.text)
print(xmlString)
如果但它返回一个空字符串。
如果我使用浏览器调用 api 并输入凭据,我会收到以下响应。
<Response>
<status>SUCCESS</status>
<callId>99999903219032190321</callId>
<result xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="Dummy">
<authorFullName>jack jones</authorFullName>
<authorOrderNumber>1</authorOrderNumber>
</result>
</Response>
谁能告诉我哪里出错了。
原文由 MrG 发布,翻译遵循 CC BY-SA 4.0 许可协议
虽然这不是 OP 的确切答案,但它可以解决
blank response
来自python-requests
的人的问题。由于内容类型错误,我收到了空白回复。我期待的是 HTML 而不是 JSON 或登录成功。正确的
content-type
对我来说是application/x-www-form-urlencoded
。基本上我必须执行以下操作才能使我的脚本正常工作。
在 application/x-www-form-urlencoded 或 multipart/form-data? 中了解更多相关信息?