Python中的POST HTTP:保留的XML名称。 line:2,char:40

时间:2015-01-21 13:19:47

标签: python xml http post

我正在尝试将德国支付提供商SOFORTÜberweisung整合到我自己的Python编码在线商店中。他们没有可用的Python库,他们的支持也无法解决出错的问题。

他们要求在第一步中发布XML,如下所示:

<?xml version="1.0" encoding="UTF-8" ?>
<multipay>
      <project_id>WHITEOUT</project_id>
      <amount>24.51</amount>
      <currency_code>EUR</currency_code>
      <reasons>
            <reason>Testueberweisung</reason>
            <reason>-TRANSACTION-</reason>
      </reasons>
      <success_url>http://WHITEOUT?stage=paymentAuthorizationSuccessful</success_url>
      <success_link_redirect>1</success_link_redirect>
      <abort_url>http://WHITEOUT?stage=paymentCancelled</abort_url>
      <su />
</multipay>

我的代码实现如下:

response = PostHTTP(url = self.endpoint, data = xml, authentication = '%s:%s' % (self.clientNumber, self.APIkey), contentType = 'application/xml; charset=UTF-8')

使用以下功能:

def PostHTTP(url, values = [], data = None, authentication = None, contentType = None):
u"""\
POST HTTP responses from the net. Values are dictionary {argument: value}
Authentication as "username:password"
"""

import urllib, urllib2, base64

if values:
    data = urllib.urlencode(values)

headers = {}


if contentType:
    headers["Content-Type"] = contentType
    headers["Accept"] = contentType

if authentication:
    base64string = base64.encodestring(authentication)
    headers["Authorization"] = "Basic %s" % base64string

request = urllib2.Request(url, data, headers)
response = urllib2.urlopen(request)
return response.read()

他们的服务器继续使用

进行响应
<?xml version="1.0" encoding="UTF-8"?>
<errors>
  <error>
    <code>7000</code>
    <message>Reserved XML Name. line: 2, char: 40</message>
  </error>
</errors>

我不明白。 有什么想法吗?

更新

他们的支持再次得到回应。 似乎错误确实在服务器端,对于更改,因为当我省略第一行<?xml ?>时,响应是预期的。

1 个答案:

答案 0 :(得分:1)

似乎xml部分不在Request的第一行。

[empty-line]
<?xml version="1.0" encoding="UTF-8" ?>
<multipay>
  <project_id>WHITEOUT</project_id>
  <amount>24.51</amount>
  <currency_code>EUR</currency_code>
  <reasons>
        <reason>Testueberweisung</reason>
        <reason>-TRANSACTION-</reason>
  </reasons>
  <success_url>http://WHITEOUT?stage=paymentAuthorizationSuccessful</success_url>
  <success_link_redirect>1</success_link_redirect>
  <abort_url>http://WHITEOUT?stage=paymentCancelled</abort_url>
  <su />
</multipay>

尝试删除前导(和尾随)空格。 这应该可以解决问题。