【发布时间】:2014-12-04 15:26:56
【问题描述】:
我的任务是编写一个与 ClearBooks 的 API 交互的程序。他们有一些documentation,但我仍然很难开始。根据我从文档中收集到的信息,为了接收看似神秘的“响应 200”,我需要:
- 向“https://secure.clearbooks.co.uk/api/soap/”发布“XML 请求”
- 将“请求 http 标头”设置为“Content-Type: text/xml”
- 在我的 API 密钥(我在本地拥有)中包含一个“SOAP 标头”
我目前的代码如下:
import requests
url = "https://secure.clearbooks.co.uk/api/soap/"
headers = {"Content-Type": "text/xml", "apiKey": "(api key goes here)"}
response = requests.get(url=url, headers=headers)
print(response)
print(response.text)
>>> <Response [500]>
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"><SOAP-ENV:Body><SOAP-ENV:Fault><faultcode>Sender</faultcode><faultstring>Invalid XML</faultstring></SOAP-ENV:Fault></SOAP-ENV:Body></SOAP-ENV:Envelope>
我知道我的代码可能看起来很愚蠢,但这是我第一次尝试编写与 Internet 交互的程序,尽管我花了几天时间尝试破解它,但这是我我发现真的很难 - 我已经设法从通用网页中抓取文本,但没有设法与没有 Codecademy 或类似的任何 API 进行交互。请问有人可以更新我的代码(或者更有可能从头开始)以便它工作吗?我知道如果没有自己的 Clearbooks API 密钥,您就无法测试它,但我们将非常感谢您提供任何帮助。
【问题讨论】:
标签: python api http soap python-requests