查找 GET 请求是否找不到资源的最简单方法
Easiest way to find if a GET request does not find a resource
在python-eve framework, what would be the easiest way to find in a post GET hook that the GET request failed to find any resources (for example, the given filtering parameters中没有匹配到任何资源)?
谢谢!
您应该能够阅读挂钩中的响应 header X-Total-Count。如果它的值为 0,那么它找不到任何资源。
要访问此值,调用类似于:
# Within run.py
app.on_fetched_resource_something += before_returning_something_items
# Within your hook module file
def before_returning_something_items(response):
numRecs = int(response.headers.get("X-Total-Count"))
if numRecs > 0:
print "Found Something"
else:
print "Found Zilcho!"
由于 payload
是一个 Flask Response
对象,您可以利用它的特性。一种选择是简单地调查 _items
键,在收集端点上,returns 实际文档:
import json
def on_post_get(resoure, request, payload):
# get the actual response json out of Flask Response
json = json.loads(payload.get_data())
documents = json['_items']
assert(len(documents) == 0)
app = Eve()
app.on_post_GET += on_post_get
if __name__ == '__main__':
app.run()
在python-eve framework, what would be the easiest way to find in a post GET hook that the GET request failed to find any resources (for example, the given filtering parameters中没有匹配到任何资源)?
谢谢!
您应该能够阅读挂钩中的响应 header X-Total-Count。如果它的值为 0,那么它找不到任何资源。
要访问此值,调用类似于:
# Within run.py
app.on_fetched_resource_something += before_returning_something_items
# Within your hook module file
def before_returning_something_items(response):
numRecs = int(response.headers.get("X-Total-Count"))
if numRecs > 0:
print "Found Something"
else:
print "Found Zilcho!"
由于 payload
是一个 Flask Response
对象,您可以利用它的特性。一种选择是简单地调查 _items
键,在收集端点上,returns 实际文档:
import json
def on_post_get(resoure, request, payload):
# get the actual response json out of Flask Response
json = json.loads(payload.get_data())
documents = json['_items']
assert(len(documents) == 0)
app = Eve()
app.on_post_GET += on_post_get
if __name__ == '__main__':
app.run()