简体   繁体   English

AWS Api Gateway 模拟集成响应标头映射,如何?

[英]AWS Api Gateway mock integration response header mapping, how?

I was digging web and kept trying to find what I am missing in the following situation: The basic problem I am trying to solve is: when http://example.com/login?target=http://google.com is called, the user is redirected to an OAuth login page with the state referring to the url in the target query param.我正在挖掘网络并一直试图找到在以下情况下我缺少的东西:我试图解决的基本问题是:当http://example.com/login?target=http://google.com被调用时,用户被重定向到 OAuth 登录页面,其状态指的是目标查询参数中的 url。

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

I could do this with Lambda, but I felt it is unnecessary and API Gateway could be used solely.我可以用 Lambda 做到这一点,但我觉得没有必要,可以单独使用 API Gateway。

First I created the /login resource and a GET method, to which I added a mock integration.首先,我创建了/login资源和一个GET方法,并向其中添加了一个模拟集成。

集成请求

I set 302 at Method Response:我在方法响应中设置了 302:

方法响应

Finally I added Integration Response:最后我添加了集成响应:

整合响应

However when I test the method, the Location header is missing.但是,当我测试该方法时,缺少 Location 标头。 If I put a static string to the Location header in Integration Response it works, but whatever I try at the header mapping it is not working.如果我在集成响应中的位置标头中放置一个静态字符串,它会起作用,但是无论我在标头映射中尝试什么,它都不起作用。

Things I tried at Integration Request mapping:我在集成请求映射中尝试过的事情:

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

I seems that integration.response.body.target or integration.response.header.target is always empty.我似乎integration.response.body.targetintegration.response.header.target总是空的。 According to the documentation, headers property can be part of the integration response, but I cannot get any value from the Mapping Template I set at Integration Request.根据文档, headers属性可以是集成响应的一部分,但我无法从我在集成请求中设置的映射模板中获得任何值。

Any idea what I did incorrectly?知道我做错了什么吗?

You can do a response override, eg: in your integration request您可以进行响应覆盖,例如:在您的集成请求中

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

This will override your response header location这将覆盖您的响应标头位置

It looks like you're trying to map from a header value but a header value is never being set, instead you're setting a property called "headers" in the body.看起来您正在尝试从标头值进行映射,但从未设置标头值,而是在正文中设置了一个名为“标头”的属性。 In order to map a value from integration.response.header.target you're going to need to set that value in your mock in the HTTP Headers section of the Integration Request.为了映射来自integration.response.header.target的值,您需要在集成请求的 HTTP 标头部分的模拟中设置该值。

However you may find it simpler to set your request Mapping Template for the mock to但是,您可能会发现将模拟的请求映射模板设置为更简单

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

and then you could then use a response mapping to set your Location header to integration.response.body.target然后您可以使用响应映射将您的Location标头设置为integration.response.body.target

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM