如何在前夕的变量中获取相同的响应数据?

时间:2017-04-06 07:04:29

标签: python python-3.x response eve

以下是eve python框架生成的响应。

{
  "_status": "ERR",
  "_error": {
    "code": 422,
    "message": "Insertion failure: 1 document(s) contain(s) error(s)"
  },
  "_issues": {
    "_email": "value 'sbchcbhjcj@gmail.com' is not unique",
  }
}

我希望将此响应存储在变量中。

我还想在控制台上打印具有上述所有响应的变量。

1 个答案:

答案 0 :(得分:1)

您可以使用事件挂钩,使用前夕文档中描述的here之后的请求后事件挂钩来打印每种请求类型的响应。

例如,在post_POST挂钩中,您有可以打印的响应信息,如下所示:

from werkzeug.exceptions import BadRequest

def after_post_log(resource, request, r):
    try:
        log.info("Finished POST request. url={} body={}. Response={}".format(request.url, request.json, r.response))
    except BadRequest:
        log.error("Finished POST request. url={} malformed JSON body response={}".format(request.url, r.response))

app = Eve()
app.on_post_POST += after_post_log

请注意,上面代码中的log初始化被省略。