【问题标题】:How to return a custom response with CherryPy如何使用 CherryPy 返回自定义响应
【发布时间】:2017-03-26 04:40:00
【问题描述】:

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

我想控制状态码和正文。

例如在 Flask 中我可以这样做:

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

【问题讨论】:

标签: python cherrypy


【解决方案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

你也可以看看这个: http://www.programcreek.com/python/example/2969/cherrypy.response

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-03-05
    • 2019-10-28
    • 1970-01-01
    • 2021-01-08
    • 1970-01-01
    • 2021-05-10
    • 2021-07-13
    • 2020-04-23
    相关资源
    最近更新 更多