【问题标题】:how to post xml in python如何在python中发布xml
【发布时间】:2020-11-06 08:21:51
【问题描述】:

我正在尝试将字符串发布为 xml,代码如下:

     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)

来自服务器的响应:

415

我也试过这样的代码:

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

来自服务器的响应:

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

【问题讨论】:

标签: python xml python-requests


【解决方案1】:

HTTP 415 是 不支持的媒体类型 - 这表明您的请求丢失或 Content-Type 标头不正确。试试:

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

【讨论】:

    猜你喜欢
    • 2015-09-17
    • 1970-01-01
    • 2012-12-10
    • 2012-11-25
    • 1970-01-01
    • 2011-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多