how to post xml in python

Tags: , ,



i am trying to post string as xml, here is the code:

     f = "<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <MakePaymentByServiceType xmlns="KioskSystems">
      <terminalId>1</terminalId>
      <serviceType>BBC</serviceType>
      <guid>asdfasd</guid>
      <requisite>123456</requisite>
      <amount>100</amount>
      <comission>decimal</comission>
      <user_comment>string</user_comment>
      <user_tin>string</user_tin>
      <user_address>string</user_address>
    </MakePaymentByServiceType>
  </soap:Body>
</soap:Envelope>"
    soup = BeautifulSoup(f, "xml")
    xml = soup.prettify()

    requests.post(url, data=xml)

response from server:

415

i also tried code like this:

        with open('xmlfile.xml') as xml:
            r = requests.post(url, data=xml)

response from server:

    raise ConnectionError(err, request=request)
requests.exceptions.ConnectionError: ('Connection aborted.', RemoteDisconnected('Remote end closed connection without response'))

Answer

HTTP 415 is Unsupported Media Type – this suggest your request is missing or has incorrect Content-Type header. Try:

headers = {"Content-Type":"text/xml"}
r = requests.post(url, headers=headers, data=xml)


Source: stackoverflow