【问题标题】:Lambda and API gateway mappingLambda 和 API 网关映射
【发布时间】:2016-07-19 05:00:49
【问题描述】:

我想从处理程序返回一个值到 API 网关响应标头。

Handler.js

module.exports.handler = function(event, context, cb) {
  const UpdateDate = new Date();  
  return cb(null, {
    body: {
      message: 'test'
    },
    header: {
      Last-Modified: UpdateDate
    }
  });
};

“端点”中的 s-function.json

"responses": {
    "400": {
      "statusCode": "400"
    },
    "default": {
      "statusCode": "200",
      "responseParameters": {
        "method.response.header.Cache-Control": "'public, max-age=86400'",
        "method.response.header.Last-Modified": "integration.response.body.header.Last-Modified"
      },
      "responseModels": {
        "application/json;charset=UTF-8": "Empty"
      },
      "responseTemplates": {
        "application/json;charset=UTF-8": "$input.json('$.body')"
      }
    }
  }

这可以工作。但我想知道如何使用“integration.response.header.Last-Modified”。我的处理程序回调格式错了吗?

编辑: “端点”中的 s-function.json

"integration.response.header.Last-Modified" 这不起作用。 我想知道特定的处理程序返回格式以将数据传递给“integration.response.header.Last-Modified”。

"responses": {
    "400": {
      "statusCode": "400"
    },
    "default": {
      "statusCode": "200",
      "responseParameters": {
        "method.response.header.Cache-Control": "'public, max-age=86400'",
        "method.response.header.Last-Modified": "integration.response.header.Last-Modified"
      },
      "responseModels": {
        "application/json;charset=UTF-8": "Empty"
      },
      "responseTemplates": {
        "application/json;charset=UTF-8": "$input.json('$.body')"
      }
    }
  }

【问题讨论】:

  • 乍一看这看起来是正确的,但我不明白你的问题。你是说这不起作用吗?如果没有,您看到了什么行为?
  • 我想使用“integration.response.header.Last-Modified”,但我不知道 lambda 返回甲酸盐。

标签: aws-lambda aws-api-gateway serverless-framework


【解决方案1】:

您的 lambda 函数的所有输出都在响应正文中返回,因此您需要将部分响应正文映射到您的 API 响应标头。

module.exports.handler = function(event, context, cb) {
  const UpdateDate = new Date();  
  return cb(null, {
      message: 'test',
      Last-Modified: UpdateDate
  });
};

将产生有效载荷 "{"message" : "test", "Last-Modified" : "..."}"

在这种情况下,您将使用“integration.response.body.Last-Modified”作为映射表达式。附带说明一下,在响应正文中命名“正文”和“标题”可能会使映射表达式难以阅读。

谢谢, 瑞恩

【讨论】:

  • 谢谢!这对我很有帮助。
猜你喜欢
  • 1970-01-01
  • 2022-07-14
  • 2019-05-02
  • 2019-07-11
  • 1970-01-01
  • 1970-01-01
  • 2021-09-12
  • 1970-01-01
  • 2020-11-29
相关资源
最近更新 更多