7

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?

1
  • You can open any file and send the data it contains as a string, if that's what you're asking. Commented Apr 24, 2013 at 12:58

2 Answers 2

6

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.