Simple XML via http web service

I have a simple html service developed in django. You enter your name - it posts this and returns a value (male / female).

I need to do this as a web service. I don't know where to start.

I want to accept an xml request and provide an xml response - here it is.

Can anyone provide any pointers - Googling is difficult unless you know what you are looking for.

+2


a source to share


2 answers


For instructions, see Creating Non-HTML Content in the django book.

Basically, it's that simple:

def get_data(request, xml_data):
    data = parse_xml_data(xml_data)
    return_data = create_xml_blob(data)
    return HttpResponse(return_data, mimetype='application/xml')

      

Edit:



You can send a message with xml_data set to an XML string, or you can send an XML request.

Here's the code to send XML data to the web service, adapted from this site :

xml_data = """<?xml version="1.0" encoding="UTF-8"?>
<root>my data here</root>
"""

#construct and send the header

webservice = httplib.HTTP("example.com")
webservice.putrequest("POST", "/rcx-ws/rcx")
webservice.putheader("Host", "example.com")
webservice.putheader("User-Agent", "Python post")
webservice.putheader("Content-type", "text/xml; charset=\"UTF-8\"")
webservice.putheader("Content-length", "%d" % len(xml_data))
webservice.endheaders()
webservice.send(xml_data)

      

From django you can use request.raw_post_data

to access XML directly.

+1


a source


You probably want Piston , which is the basis for exposing Django apps as web services.



+2


a source







All Articles