How can I send an xml body using requests library?

前端 未结 2 1380
野的像风
野的像风 2020-11-27 16:19
def request():
    #encoded_xml = urllib.urlencode({\'XML\': read_xml()})
    #encoded_xml = read_xml()
    headers = {\'Authorization\': AUTH_TOKEN,\\
                      


        
相关标签:
2条回答
  • 2020-11-27 16:25

    Pass in the straight XML instead of a dictionary.

    0 讨论(0)
  • 2020-11-27 16:28

    Just send xml bytes directly:

    #!/usr/bin/env python2
    # -*- coding: utf-8 -*-
    import requests
    
    xml = """<?xml version='1.0' encoding='utf-8'?>
    <a>б</a>"""
    headers = {'Content-Type': 'application/xml'} # set what your server accepts
    print requests.post('http://httpbin.org/post', data=xml, headers=headers).text
    

    Output

    {
      "origin": "x.x.x.x",
      "files": {},
      "form": {},
      "url": "http://httpbin.org/post",
      "args": {},
      "headers": {
        "Content-Length": "48",
        "Accept-Encoding": "identity, deflate, compress, gzip",
        "Connection": "keep-alive",
        "Accept": "*/*",
        "User-Agent": "python-requests/0.13.9 CPython/2.7.3 Linux/3.2.0-30-generic",
        "Host": "httpbin.org",
        "Content-Type": "application/xml"
      },
      "json": null,
      "data": "<?xml version='1.0' encoding='utf-8'?>\n<a>\u0431</a>"
    }
    
    0 讨论(0)
提交回复
热议问题