Yanone
Yanone

Reputation: 185

POST HTTP in Python: Reserved XML Name. line: 2, char: 40

I'm trying to integrate German payments provider SOFORT Überweisung into my own, Python-coded online shop. They have no Python libs available and their support also can't answer what is going wrong.

They require XML to be POSTed in the first step like so:

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

My code to achieve this is the following:

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

using the following function:

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()

Their server keeps on responding with

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

and I don't get it. Any ideas?

UPDATE:

Their support answered again. It seems that the error was indeed on the server side, for a change, because when I leave out the first line <?xml ?> the response is as expected.

Upvotes: 1

Views: 78

Answers (1)

lxlang
lxlang

Reputation: 11

Seems like the xml part is not in the first line of the 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>

Try to remove leading (and trailing) whitespaces. That should solve the Problem.

Upvotes: 1

Related Questions