2011-05-06 150 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