2016-11-11 24 views
1

如何使用CherryPy返回自定义响应?如何使用CherryPy返回自定义响应

我想控制状态码和正文。

例如,在烧瓶我可以这样做:

return Response(response=message, status=200, mimetype="application/json")

+0

它可能通过'cherrypy.response'。对于json,您还可以将'@ cherrypy.tools.json_out()'装饰器应用于您的请求处理程序并返回一个字典。 – webKnjaZ

+0

有没有这方面的文件,我找不到任何操纵回应 –

+0

https://github.com/GDG-Ukraine/gdg.org.ua/blob/9a910e74d2ea73e96b3feec02d8412c95e67dbe4/src/GDGUkraine/errors.py#L44- L46 https://github.com/GDG-Ukraine/gdg.org.ua/blob/f682470f3d027ec41b6aeee9750c999dc535afec/src/GDGUkraine/rest_controller.py#L452-L459 – webKnjaZ

回答

1

这里是返回编码的自定义JSON数据包

result={ 
    'some': "random", 
    'data': [] 
} 
return datastore.json.dumps(result) 

这里的一个例子是恢复一个生成的PDF的例子(改变标题)

cherrypy.response.headers['Content-Type'] = 'application/pdf' 
cherrypy.response.headers['Content-Disposition'] = 'inline;filename="report.pdf"' 
return pdfblob 

You co üld也看看这个: http://www.programcreek.com/python/example/2969/cherrypy.response

相关问题