Home

Setting Response Header Content-type from Python / Cherrypy

  • by Sergio Oliveira
  • Classification: Code Snippet
  • Applies to: Python, CherryPy, Content-type

import cherrypy
from xml.dom import minidom

class Root:
    def index(self):

        # Create a new xml document*
        xml = minidom.Document()
        userElem = xml.createElement("user")
        userElem.setAttribute("name", "Sergio Oliveira")
        userElem.setAttribute("nickname", "seocam")
        userElem.setAttribute("email", "seocam@taboca.com")
        userElem.setAttribute("photo","seocam.png")
        xml.appendChild(userElem)
 
        # Change the content type to 'text/xml'
        cherrypy.response.headers['Content-Type'] = "text/xml"
        return xml.toxml("UTF-8")
    index.exposed = True

cherrypy.root = Root()
cherrypy.server.start()