views:

151

answers:

3

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 ofer this as a web service. I have no idea where to start.

I want to accept a xml request, and provide an xml response - thats it.

Can anyone give ma any pointers - Googling it is difficult when you dont know what your searching for.

+2  A: 

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

Daniel Roseman
Yeah, definitely +1 for piston
stevejalim
+1  A: 

See the Generating non-HTML content in the django book for instructions.

Basically, it's as simple as this:

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 post with xml_data set to the XML string, or you can send an XML request.

Here's some code for sending XML data to a 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'd use request.raw_post_data to get at the XML directly.

Ryan Ginstrom
I am not worried about generating non-html content. Accepting an xml request is what I am trying to do.
Mark
Send it in a post, with the variable xml_data set to your xml data. Encode in utf-8.
Ryan Ginstrom
A: 

You can check this

anijhaw