我想这是一个比eBay特定的普遍问题,但我不确定:我正在尝试向eBay开发者API发送XML请求以检索XML响应。当使用卷曲,一切工作正常,我也得到一个XML响应告诉我,API密钥缺失哪些(如果我会通过HTTP报头提供他们,我会得到一个有效的XML结果):如何通过Python向eBay API发送有效的XML POST请求?
curl -d '<!xml version="1.0" encoding="utf-8"?><findItemsByKeywordsRequest xmlns="http://www.ebay.com/marketplace/search/v1/services"><keywords>harry potter phoenix</keywords></findItemsByKeywordsRequest>' \
http://svcs.sandbox.ebay.com/services/search/FindingService/v1
这导致了正确答案:
<?xml version='1.0' encoding='UTF-8'?>
<ms:errorMessage xmlns:ms="http://www.ebay.com/marketplace/services" xmlns="http://www.ebay.com/marketplace/search/v1/services">
<error>
<errorId>2038</errorId>
<domain>CoreRuntime</domain>
<severity>Error</severity>
<category>System</category>
<message>Missing SOA operation name header</message>
<subdomain>System</subdomain>
</error>
</ms:errorMessage>
但是当我尝试使用Python工作,我只是得到“500内部服务器错误”,无论我怎么做基础我的例子。我已经尝试了两个非常基本的方法:
第一:
serverUrl = 'svcs.sandbox.ebay.com'
xmlparameters = '<!xml version="1.0" encoding="utf-8"?><findItemsByKeywordsRequest xmlns="http://www.ebay.com/marketplace/search/v1/services"><keywords>harry potter phoenix</keywords></findItemsByKeywordsRequest>'
webservice = httplib.HTTP(serverUrl)
webservice.putrequest("POST", "/services/search/FindingService/v1")
webservice.putheader("Host", serverUrl)
webservice.putheader("Content-type", "text/xml; charset=\"UTF-8\"")
webservice.putheader("Content-length", "%d" % len(xmlparameters))
webservice.endheaders()
webservice.send(xmlparameters)
排名第二(这是我的首选方法):
serverUrl = 'svcs.sandbox.ebay.com'
xmlparameters = '<!xml version="1.0" encoding="utf-8"?><findItemsByKeywordsRequest xmlns="http://www.ebay.com/marketplace/search/v1/services"><keywords>harry potter phoenix</keywords></findItemsByKeywordsRequest>'
connection = httplib.HTTPConnection(serverUrl)
connection.request("POST", '/services/search/FindingService/v1', xmlparameters)
正如你可以在卷曲例子中看到的,但它确实无论我不发送API密钥等,它都应该返回一个XML错误响应,而不仅仅是HTTP状态代码“500 Internal server error”。
有没有人看到我做错了我的POST请求?
[编辑] btw使用URL ValueName API与Python完美协作,但这只是一个URL上的GET请求。但是,我更愿意使用XML API。但是,如果这是不可能的,我当然会切换到ValueName URI。
你是对的...在这种情况下,我们在德国说“没有看到所有的树林”。总是如此;)多么尴尬。非常感谢!会给你一个upvote,但我必须首先考虑一个好的OpenID用户名:) – Aufziehvogel 2011-05-06 18:46:38
没问题,祝你好运:D – zeekay 2011-05-06 18:51:30
有些人会在几个月内发现这个问题:ebay不希望在开始时使用XML字符串,否则会返回错误“无法为XML创建XML流读取器:有效内容格式不正确或有效内容为空”。在开始时删除XML标签可以使所有的工作都正常。 – Aufziehvogel 2011-05-06 18:54:30