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?
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().
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With