Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Post XML file using Python

Tags:

python

xml

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?

like image 303
Krisso123 Avatar asked Apr 24 '13 12:04

Krisso123


People also ask

How do I load XML in Python?

To read an XML file using ElementTree, firstly, we import the ElementTree class found inside xml library, under the name ET (common convension). Then passed the filename of the xml file to the ElementTree. parse() method, to enable parsing of our xml file. Then got the root (parent tag) of our xml file using getroot().

Can you use XML with Python?

The Python standard library provides a minimal but useful set of interfaces to work with XML. The two most basic and broadly used APIs to XML data are the SAX and DOM interfaces. Simple API for XML (SAX) − Here, you register callbacks for events of interest and then let the parser proceed through the document.

How do I send an XML request?

Send XML requests with the raw data type, then set the Content-Type to text/xml . After creating a request, use the dropdown to change the request type to POST. Open the Body tab and check the data type for raw. Click Send to submit your XML Request to the specified server.

How do I save an XML file in Python?

Write XML to the writer object. The writer should have a write() method which matches that of the file object interface. The indent parameter is the indentation of the current node. The addindent parameter is the incremental indentation to use for subnodes of the current one.


1 Answers

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.

like image 187
eandersson Avatar answered Oct 03 '22 11:10

eandersson