繁体   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