【发布时间】:2015-10-09 18:37:42
【问题描述】:
在我的 Werkzeug 应用程序中,我正在拦截所有错误响应并尝试使用 JSON 响应来响应,如果客户端需要 JSON 或返回带有 404 或 500 的常用 HTML 页面:
def handle_error_response(self, environ, start_response, exc):
if ('application/json' in environ.get('CONTENT_TYPE', '')
and exc.get_response().content_type != 'application/json'):
start_response('%s %s' % (exc.code, exc.name),
(('Content-Type', 'application/json'), ))
return (json.dumps({"success": False, "error": exc.description}, ensure_ascii=False), )
# go the regular path
...
在这个解决方案中,我依赖于包含字符串'application/json' 的 Content-Type 标头。
但这看起来不像是一个正确的解决方案,因为维基百科说:
Content-Type请求正文的 MIME 类型(用于 POST 和 PUT 请求)
检查'text/html' 是否在标头Accept 中然后返回HTML 响应,否则返回JSON 响应是一个好策略吗?
还有其他更强大的解决方案吗?
当 Chrome 请求 HTML 页面标题时
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
在 Ember 发出 API 请求时发送
Accept: application/json, text/javascript, */*; q=0.01
已发送。
也许应该考虑X-Requested-With: XMLHttpRequest?
【问题讨论】:
标签: python http http-headers mime-types