2016-11-11 25 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

相關問題