Return json 来自 cherrypy before_handler 工具
Return json from cherrypy before_handler tool
我有一个 cherrypy 应用程序,我在其中实现了一个附加到 before_handler 挂钩的自定义工具(请求过滤器)。
下面是过滤器的实现:
def custom_filter():
method = cherrypy.request.method
if method == 'POST':
print 'check POST token'
try:
request_headers = cherrypy.request.headers
token = request_headers['Authorization']
if not _auth.validate_token(token):
return 'error message'
except:
print 'Error in post filter'
我想要的是在令牌无效时return向客户端发送一条消息。 return 语句无效。是否有可能做到这一点?如果没有,是否有替代方案?
基于 post,经过一些调查,我找到了一个适合我的解决方案:停止执行请求,然后添加响应主体。
def custom_filter():
method = cherrypy.request.method
if method == 'POST':
print 'check POST token'
try:
request_headers = cherrypy.request.headers
token = request_headers['Authorization']
if not _auth.validate_token(token):
cherrypy.request.handler = None # stop request
cherrypy.response.status = 403
cherrypy.response.body = 'test' # add response message
except:
print 'Error in post filter'
我有一个 cherrypy 应用程序,我在其中实现了一个附加到 before_handler 挂钩的自定义工具(请求过滤器)。 下面是过滤器的实现:
def custom_filter():
method = cherrypy.request.method
if method == 'POST':
print 'check POST token'
try:
request_headers = cherrypy.request.headers
token = request_headers['Authorization']
if not _auth.validate_token(token):
return 'error message'
except:
print 'Error in post filter'
我想要的是在令牌无效时return向客户端发送一条消息。 return 语句无效。是否有可能做到这一点?如果没有,是否有替代方案?
基于
def custom_filter():
method = cherrypy.request.method
if method == 'POST':
print 'check POST token'
try:
request_headers = cherrypy.request.headers
token = request_headers['Authorization']
if not _auth.validate_token(token):
cherrypy.request.handler = None # stop request
cherrypy.response.status = 403
cherrypy.response.body = 'test' # add response message
except:
print 'Error in post filter'