开发者

Simple XML over http web service

开发者 https://www.devze.com 2022-12-30 07:48 出处:网络
I have a simple html service, developed in django. You enter your name - it posts this, and r开发者_StackOverflow中文版eturns a value (male/female).

I have a simple html service, developed in django. You enter your name - it posts this, and r开发者_StackOverflow中文版eturns 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.


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消