2011-05-06 144 views
1

我想这是一个比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。

回答

0

它返回500个状态和XML响应:

>>> connection.request("POST", '/services/search/FindingService/v1', xmlparameters) 
>>> resp = connection.getresponse() 
>>> resp.status 
<<< 500 
>>> resp.read() 
<<< '<?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>' 
+1

你是对的...在这种情况下,我们在德国说“没有看到所有的树林”。总是如此;)多么尴尬。非常感谢!会给你一个upvote,但我必须首先考虑一个好的OpenID用户名:) – Aufziehvogel 2011-05-06 18:46:38

+0

没问题,祝你好运:D – zeekay 2011-05-06 18:51:30

+1

有些人会在几个月内发现这个问题:ebay不希望在开始时使用XML字符串,否则会返回错误“无法为XML创建XML流读取器:有效内容格式不正确或有效内容为空”。在开始时删除XML标签可以使所有的工作都正常。 – Aufziehvogel 2011-05-06 18:54:30

0

500响应状态是相当通用的,无论服务器上发生了什么错误,都应该返回。你会考虑使用CGI trackback来返回错误信息吗?

http://docs.python.org/library/cgitb.html#module-cgitb

+0

我的脚本本身没有错误,从而cgitb似乎没有返回错误。我在第一行启用了它,当我在代码中产生一个错误(例如调用一个不存在的变量)时,它返回一个回溯,但是使用我当前的代码,整个脚本运行良好:“>>> ebaypredict。 sendRequest() 发送请求错误:500内部服务器错误“(由一个函数产生,检查状态码是否正确)。 – Aufziehvogel 2011-05-06 17:54:29

+0

我想你以为我正在使用我的网络服务器,因此推荐使用CGI追溯工具?我打电话给另一个网络服务器,那个人正在产生错误信息,所以我似乎以某种方式向ebay发送了错误的请求。 – Aufziehvogel 2011-05-06 18:00:20

+0

是的,我现在看得更清楚了。 – maniak1982 2011-05-06 18:20:57