cherrypy - 更改 Content-Type 后返回的内容未自动编码

给定一个具有多种表示(媒体类型)的资源,该资源不使用自定义 CherryPy“工具”来处理“接受”HTTP header 的解释和响应实体主体的序列化,CherryPy 引发以下 ValueError 在适当设置“Content-Type”HTTP header 后从页面处理程序返回内容时出现异常,但仅适用于某些媒体类型:

ValueError: Page handlers MUST return bytes. Use tools.encode if you wish to return unicode.

例子:

contentType = tools.accept.callable(media = ['application/json', 'text/html'])

if contentType == 'application/json':
    return json.dumps(studies)
elif contentType == 'text/html':
    ...

这适用于两种媒体类型,尽管 JSON 表示将被错误地声明为 HTML(默认)。

contentType = tools.accept.callable(media = ['application/json', 'text/html'])
response.headers['Content-Type'] = "{mediaType}; charset=utf-8".format(mediaType = contentType)

if contentType == 'application/json':
    return json.dumps(studies)
elif contentType == 'text/html':
    ...

此处,当将 JSON 内容作为字符串返回时会引发上述异常。

尝试确保 tools.encode 确实启用并将 tools.encode.encoding 显式设置为 utf-8(即使它是默认)失败。一切都适用于 HTML 表示,因此它似乎也适用于 JSON 表示。

目前 tools.encode 的文档似乎相当稀疏​​,因此最好的方法并不是立即显而易见。

最佳答案

CherryPy 编码工具仅在顶级媒体类型为 text (text/*) 时自动对内容进行编码。

有一种方法可以通过 encode.text_only 设置来控制它,但它是全局的,因此在返回不应该编码的内容时可能会引入问题。在撰写本文时,一个 Unresolved 问题跟踪一个功能请求,以更精细地控制此行为:#1123 .

因此,在这种特定情况下解决此问题的最合适方法是手动对内容进行编码:

contentType = tools.accept.callable(media = ['application/json', 'text/html'])
response.headers['Content-Type'] = "{mediaType}; charset=utf-8".format(mediaType = contentType)

if contentType == 'application/json':
    return json.dumps(studies).encode('utf-8')
elif contentType == 'text/html':
    ...

https://stackoverflow.com/questions/41395176/

相关文章:

css - 由于非整数宽度,媒体查询运行异常

python-3.x - 模块 'urllib' 没有属性 'request'

unity3d - 切换场景时网格会重新加载吗?

haskell - 如何使 Pipe 与 Haskell 的 Pipe 库并发?

c - 这是什么意思,我该如何纠正它 *** 检测到堆栈粉碎 *** : ./array1outpu

haskell - Machines 和 Conduits(或其他类似库)之间的概念区别是什么?

spring-mvc - 处理 org.thymeleaf.exceptions.TemplateI

class - 为什么我不能定义 `delete` 方法?

c - 这个函数被认为是可重入的吗?

compiler-construction - 这个产生式规则是否左递归?