POST XML file with requests
<p>I'm getting:</p>

<pre><code>&lt;error&gt;You have an error in your XML syntax...
</code></pre>

<p>when I run this python script I just wrote (I'm a newbie)</p>

<pre><code>import requests

xml = """xxx.xml"""

headers = {'Content-Type':'text/xml'}

r = requests.post('https://example.com/serverxml.asp', data=xml)

print (r.content);
</code></pre>

<p>Here is the content of the xxx.xml</p>

<pre><code>&lt;xml&gt;
&lt;API&gt;4.0&lt;/API&gt;
&lt;action&gt;login&lt;/action&gt;
&lt;password&gt;xxxx&lt;/password&gt;
&lt;license_number&gt;xxxxx&lt;/license_number&gt;
&lt;username&gt;xxx@xyz.com&lt;/username&gt;
&lt;training&gt;1&lt;/training&gt;
&lt;/xml&gt;
</code></pre>

<p>I know that the xml is valid because I use the same xml for a perl script and the contents are being printed back.</p>

<p>Any help will greatly appreciated as I am very new to python.</p>

<p>You want to give the XML data from a file to <code>requests.post</code>. But, this function will not open a file for you. It expects you to pass a file object to it, not a file name. You need to open the file before you call requests.post.</p>

<p>Try this:</p>

<pre><code>import requests

# Set the name of the XML file.
xml_file = "xxx.xml"

headers = {'Content-Type':'text/xml'}

# Open the XML file.
with open(xml_file) as xml:
    # Give the object representing the XML file to requests.post.
    r = requests.post('https://example.com/serverxml.asp', data=xml)

print (r.content);
</code></pre>

python python-requests