开发者

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. 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 -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. 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>h开发者_C百科arry 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".

Does anyone see what I am doing wrong with my POST request?

[EDIT] btw using the URL ValueName API works perfectly with Python, but that’s just a GET request on a URL. Yet, I’d prefer to use the XML API. However, if that’s not possible I’d switch to ValueName URIs of course.


It's returning a 500 status and the xml response:

>>> 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. Would you consider using the CGI trackback to return the error message?

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜