【问题标题】:AWS Api Gateway mock integration response header mapping, how?AWS Api Gateway 模拟集成响应标头映射,如何?
【发布时间】:2020-04-18 15:22:44
【问题描述】:

我正在挖掘网络并一直试图在以下情况下找到我缺少的东西: 我试图解决的基本问题是:当http://example.com/login?target=http://google.com 被调用时,用户被重定向到一个OAuth 登录页面,其状态引用了目标查询参数中的url。

所以:http://example.com/login?target=http://google.com 将被重定向到 http://authenticator.com/login?type=...&redirect_uri=...&client_id=...&state=http://google.com

我可以用 Lambda 做到这一点,但我觉得没有必要,可以单独使用 API 网关。

首先,我创建了 /login 资源和 GET 方法,并在其中添加了一个模拟集成。

我在方法响应处设置了 302:

最后我添加了集成响应:

但是,当我测试该方法时,缺少 Location 标头。如果我将静态字符串放在集成响应中的 Location 标头中,它可以工作,但是无论我在标头映射中尝试什么,它都不起作用。

我在集成请求映射中尝试过的事情:

{
  "statusCode": 200,
  "headers": {"target": "https://google.com"}
}
{
  "statusCode": 200,
  "target": "https://google.com"
}

我似乎 integration.response.body.targetintegration.response.header.target 总是空的。 根据文档,headers 属性可以是集成响应的一部分,但我无法从我在集成请求中设置的映射模板中获取任何值。

知道我做错了什么吗?

【问题讨论】:

  • 嗨。您是否找到了一种将响应正文与标头映射的方法。我面临与空白空间相同的问题
  • @Jennings 我在 12 月底花了几个小时在这个上,但从那以后我没有检查过。最后我最终创建了一个 Lambda 函数,它做同样的事情。

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


【解决方案1】:

您可以进行响应覆盖,例如:在您的集成请求中

#set($urlProxy = $method.request.path.proxy)
{
    "statusCode": 301,
}
#set($context.responseOverride.header.location = "https://www.foo.bar.com/$urlProxy")

这将覆盖您的响应标头位置

【讨论】:

    【解决方案2】:

    看起来您正在尝试从标头值进行映射,但从未设置标头值,而是在正文中设置了一个名为“标头”的属性。为了映射来自 integration.response.header.target 的值,您需要在集成请求的 HTTP 标头部分的模拟中设置该值。

    但是您可能会发现将模拟的请求映射模板设置为更简单

    { "statusCode": 200, "target": "https://google.com" }

    然后您可以使用响应映射将您的 Location 标头设置为 integration.response.body.target

    【讨论】:

    • 集成请求没有 MOCK 类型的“标头”部分。我试过这个,现在显示 Location 标头,但有空字符串作为值。从 Lambda 我将这个表单用于 api 网关:docs.aws.amazon.com/apigateway/latest/developerguide/… 这就是我尝试使用“headers”属性的原因。
    • 尝试使用我展示的模板并从 integration.response.body.target 而不是 integration.response.header.target 访问值
    • 这正是我所做的。测试方法时,Location 标头是一个空字符串。我也尝试使用整个身体(integration.response.body),然后我得到一个空对象({}
    猜你喜欢
    • 2018-04-15
    • 2018-05-02
    • 2018-06-09
    • 2019-11-07
    • 2016-12-15
    • 2018-12-02
    • 2018-01-20
    • 2016-01-21
    • 1970-01-01
    相关资源
    最近更新 更多