You need to set the header of the content of the response type manually, either in the application configuration using the tool response.headersor in the handler method.
There are two parameters to the handler method on the MimeDecorator page of the Cherrypy Tools wiki page.
In the body of the method:
def hello(self):
cherrypy.response.headers['Content-Type']= 'image/png'
return generate_image_data()
Or using the tool decorator in Cherrypy 3:
@cherrypy.tools.response_headers([('Content-Type', 'image/png')])
def hello(self):
return generate_image_data()
Wikis also define a custom decorator:
def mimetype(type):
def decorate(func):
def wrapper(*args, **kwargs):
cherrypy.response.headers['Content-Type'] = type
return func(*args, **kwargs)
return wrapper
return decorate
class MyClass:
@mimetype("image/png")
def hello(self):
return generate_image_data()
source
share