Krisso123
Krisso123

Reputation: 175

Post XML file using Python

I'm new to Python and in need of some help. My aim is to send some XML with a post request to a URL, which is going to trigger a SMS being sent.

I have a small XML document that I want to post to the URL. Can I reference the XML document on my server in the python code that needs posting, or do I include the XML data to be sent in the actual python code. Can any help me out with an example?

Upvotes: 7

Views: 17831

Answers (2)

Felipe
Felipe

Reputation: 3149

If you don't want use an outside library, you can just urllib2. See this answer for an example of how to do so.

To extract the XML from the file you just have to do

XML_STRING = open('path/to/xml_file').read()

Upvotes: 2

eandersson
eandersson

Reputation: 26342

If you need to send XML I would recommend that you take a look at requests. It allows you to easily send data using POST requests.

You should be able to transmit the XML data directly from your Python code using requests.

xml = """my xml"""
headers = {'Content-Type': 'application/xml'}
requests.post('http://www.my-website.net/xml', data=xml, headers=headers)

You could also load the xml from a text-file and send that, if you don't want to have the xml document hard-coded.

Upvotes: 6

Related Questions