简体   繁体   English

如何通过Python将有效的XML POST请求发送到ebay API?

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

I guess this is rather a general problem than ebay specific, but I am not sure: I am trying to send an XML request to the ebay developer API to retrieve an XML response. 我猜这是一个普遍的问题,而不是特定于ebay的问题,但是我不确定:我正在尝试向ebay开发人员API发送XML请求以检索XML响应。 When using curl, everything works fine and I get an XML response telling me which API-keys are missing (if I would provide them via HTTP headers I would get a valid XML result): 使用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

Which leads to the correct response: 导致正确的响应:

<?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>

But when I try to work with Python I just get "500 Internal Server error", no matter how basic I make my examples. 但是,当我尝试使用Python时,无论我编写示例的基础如何,都只会收到“ 500 Internal Server error”(500内部服务器错误)。 I have tried two very basic methods: 我尝试了两种非常基本的方法:

Number one: 第一:

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)

Number two (which is my prefered method): 第二(这是我的首选方法):

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)

As you can see in the CURL example, it does not matter that I do not send the API keys etc., it should return an XML error response anyway and not only HTTP status code "500 Internal server error". 在CURL示例中可以看到,我不发送API密钥等都没关系,无论如何它应该返回XML错误响应,而不仅仅是HTTP状态代码“ 500 Internal server error”。

Does anyone see what I am doing wrong with my POST request? 有人看到我的POST请求在做什么吗?

[EDIT] btw using the URL ValueName API works perfectly with Python, but that's just a GET request on a URL. [EDIT]使用URL ValueName API的btw与Python完美配合,但这只是URL上的GET请求。 Yet, I'd prefer to use the XML API. 但是,我更喜欢使用XML API。 However, if that's not possible I'd switch to ValueName URIs of course. 但是,如果那不可能,我当然会切换到ValueName URI。

It's returning a 500 status and the xml response: 它返回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>'

The 500 response status is rather generic and should come back no matter what error is being thrown on the server. 500响应状态相当普通,无论服务器上抛出什么错误,它都应该返回。 Would you consider using the CGI trackback to return the error message? 您是否考虑使用CGI引用返回错误消息?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM