给定一个具有多种表示(媒体类型)的资源,该资源不使用自定义 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/