簡體   English   中英

如何通過Python將有效的XML POST請求發送到ebay API?

[英]Howto send a valid XML POST request to ebay API via Python?

我猜這是一個普遍的問題,而不是特定於ebay的問題,但是我不確定:我正在嘗試向ebay開發人員API發送XML請求以檢索XML響應。 使用curl時,一切正常,我得到一個XML響應,告訴我缺少哪些API密鑰(如果我將通過HTTP標頭提供它們,那么我將獲得有效的XML結果):

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 Internal Server error”(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)

在CURL示例中可以看到,我不發送API密鑰等都沒關系,無論如何它應該返回XML錯誤響應,而不僅僅是HTTP狀態代碼“ 500 Internal server error”。

有人看到我的POST請求在做什么嗎?

[EDIT]使用URL ValueName API的btw與Python完美配合,但這只是URL上的GET請求。 但是,我更喜歡使用XML API。 但是,如果那不可能,我當然會切換到ValueName URI。

它返回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>'

500響應狀態相當普通,無論服務器上拋出什么錯誤,它都應該返回。 您是否考慮使用CGI引用返回錯誤消息?

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM