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

Returned content not encoded automatically after changing Content-Type

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

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 设置来控制它,但它是全局的,因此在返回不应该编码的内容时可能会引入问题。在撰写本文时,一个未解决的问题跟踪一个功能请求以更精细地控制此行为:#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':
    ...