3
votes

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.

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

I could do this with Lambda, but I felt it is unnecessary and API Gateway could be used solely.

First I created the /login resource and a GET method, to which I added a mock integration.

Integration Request

I set 302 at Method Response:

Method Response

Finally I added Integration Response:

Integration Response

However when I test the method, the Location header is missing. 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. 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.

Any idea what I did incorrectly?

2
Hi. Did you find a way to map response body with header.I face the same issue it comes with empty space - Jennings
@Jennings I spent a few more hours on this one at the end of December, but I have not checked it since then. Finally I ended up creating a Lambda function which does the same thing. - Senki1988

2 Answers

1
votes

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

0
votes

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.

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