使用无服务器框架模拟响应数据 [英] Mock response data with serverless framework

查看:59
本文介绍了使用无服务器框架模拟响应数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

文档给出了以下作为模拟示例:

The docs give the following as a mock example:

functions:
  hello:
    handler: handler.hello
    events:
      - http:
          path: hello
          cors: true
          method: get
          integration: mock
          request:
            template:
              application/json: '{"statusCode": 200}'
          response:
            template: $input.path('$')
            statusCodes:
              201:
                pattern: ''

这确实创建了一个模拟响应......除了它是空的.

This does create a mock response...except it's empty.

我如何才能真正返回数据?我尝试将 application/json: {...} 添加到 template,但这不起作用,我尝试在 statusCodes 下添加正文 但也没有运气.

How can I actually return data here? I've tried adding application/json: {...} to template, but that doesn't work, I've tried adding a body under statusCodes but no luck there either.

似乎没有关于此的任何文档......我怎样才能返回一个实际的身体?

There doesn't seem to be any documentation on this...how can I return an actual body?

推荐答案

您可以通过设置 response.template 的值来做到这一点.但是,这不是使用 application/json 键(如 request )完成的,您只需直接设置 template.

You can do this by setting the value of response.template. However, this isn't done using an application/json key like request is, you just set template directly.

返回一个字符串foo

response:
  template: "foo"
    statusCodes:
      201:
      pattern: ''

返回 JSON

response:
  template: ${file(foo.txt)}
    statusCodes:
      201:
      pattern: ''


# Where foo.txt contains regular JSON

{
  "foo":"bar"
}

这篇关于使用无服务器框架模拟响应数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆