【问题标题】:Accessing custom headers in aws apigateway integration response (custom lambda integration)在 aws apigateway 集成响应中访问自定义标头(自定义 lambda 集成)
【发布时间】:2020-09-15 04:18:25
【问题描述】:

我有一个位于 lambda 前面的 api 网关端点(不使用 lambda 代理集成,使用自定义 lambda 集成)。 lambda 返回以下内容:

module.exports.handler = async (event, context) => {

 const response = {
     statusCode: 200,
     body: JSON.stringify({
         message: 'some message'
     }),
     headers: {
         bob: 'this is bob'
     },
 };

 return response;
};

我正在尝试在集成请求中访问标题 bob:integration.response.header.bob。这行不通。我可以像 integration.response.body.headers.bob 这样从身体中取出它。我假设前者不起作用,因为我没有使用 lambda 代理集成?还注意到我可以检索 integration.response.header.Content-Type。有人可以解释一下如何像上面的代码一样正确地添加从 lambda 传递的自定义标头(或者是像我尝试过的那样从正文中获取它的唯一选择)吗?

【问题讨论】:

    标签: amazon-web-services aws-lambda aws-api-gateway


    【解决方案1】:

    集成标头 (integration.header.header-name) 是 Lambda 服务发送到 API Gateway 的标头。我们无法控制这些标头。这些是标准标头 - Content-Type、Date 等,是“端点响应标头”的一部分。

    而您控制的自定义标头是“端点响应正文”的一部分。要回答您的问题 - 是的,您必须从正文中检索自定义标题。

    查看下面 API Gateway 执行日志的 sn-p 看看有什么不同。

    Mon Jun 22 04:28:51 UTC 2020 : Endpoint response headers: {Date=Mon, 22 Jun 2020 04:28:51 GMT, Content-Type=application/json, Content-Length=72, Connection=keep-alive, x-amzn-RequestId=4d6d78b5-bb45-4e8d-893f-a409563e493c, x-amzn-Remapped-Content-Length=0, X-Amz-Executed-Version=$LATEST, X-Amzn-Trace-Id=root=1-5ef03382-d494d01d575754f5b551dfd3;sampled=0}
    Mon Jun 22 04:28:51 UTC 2020 : Endpoint response body before transformations: {"statusCode": 200, "body": "Hello!", "headers": {"bob": "this is bob"}}
    Mon Jun 22 04:28:51 UTC 2020 : Method response body after transformations: {"statusCode": 200, "body": "Hello!", "headers": {"bob": "this is bob"}}
    Mon Jun 22 04:28:51 UTC 2020 : Method response headers: {X-Amzn-Trace-Id=Root=1-5ef03382-d494d01d575754f5b551dfd3;Sampled=0, Access-Control-Allow-Origin=*, Content-Type=application/json}
    Mon Jun 22 04:28:51 UTC 2020 : Successfully completed execution
    Mon Jun 22 04:28:51 UTC 2020 : Method completed with status: 200
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-20
      • 2018-02-17
      • 2021-09-27
      • 2018-07-27
      • 2015-07-16
      • 2022-07-13
      • 2019-12-24
      • 2022-12-11
      相关资源
      最近更新 更多