return a 302 from AWS API Gateway using request path - amazon-web-services

I have a simple site running on Lambda, using a single proxy resource in API Gateway to route requests to it.
I'm trying to set up some "vanity URLs": someone arriving on /somepath should be redirected to /?source=somepath.
I'm trying to achieve this in API Gateway rather than passing it through to the Lambda app to manage.
I've set up a resource on /somepath, with a Mock integration type.
The Method Response contains only a 302 type, which contains a Location response header.
The Integration Response contains a corresponding 302 response type
The Header Mappings contain a Location header mapped to context.resourcePath
This works, sort of. A request to /somepath returns a 302 response with a Location header value of /somepath.
But how can I format that Header Mapping value to be (i.e.):
/?source=${context.resourcePath}
Is there a way I can do this all in API Gateway, using only a Mock endpoint instead of passing the request through to an actual integration backend to return a header value?

Related

APIGateway mapping header to path via proxy

I am trying to proxy a request though API gateway to an internal service.
My external request looks like:
Path: /resources
Headers: owner_id passed along which was decoded via an authorizer in a previous step.
The proxied endpoint I would like to hit is:
/owner/{owner_id}/resources
Is it possible to pull a header out and use this as a path parameter to the internal API?
Set an Endpoint URL like http://httpbin.org/anything/{owner_id}/blah1/blah2 in Integration Request
in the same Integration Request, add a Mapping Templates : "When there are not templates defined (recommended)) section, add a Content-Type : application/json with a template body of #set($context.requestOverride.path.owner_id = $input.params("owner_id"))
See also: https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-mapping-template-reference.html#input-variable-reference

AWS API Gateway Not Passing Custom Headers

I have an API Gateway request where I am passing a custom header that is not being passed into the Lambda which is its target. First and foremost, I checked the verbose logs for the API Gateway Instance and can see the custom header called x-treasury-system being passed in and present in Method request headers:
But it is not present in Endpoint request headers:
Which is why I think its not ending up in the Lambda. I have the following header mappings setup - In the Method Request:
And in the Integration Request:
What am I missing? It seems that all is in order to pass that header, I double checked in lambda logs when printing out the headers that it is for sure not coming in.

AWS API Gateway integration request Http headers not being passed to lambda

This is similar to this post API-Gateway Integration Request HTTP Header not mapping query string to header but I can't see any answer for it and all of the answers are not related to what the question is intended.
I am trying to add the integration request parameters in API Gateway, but whenever I set 'Http Headers' as shown here and 'Mapping template' as passthrough, I could not see that header when I log inside the lambda.
Also in the integration response I cannot reference it inside the integration header response parameters. The "integration.response.header.cokers" will be returned blank when I invoke the API. This is how I configured the integration response
Ultimately the solution here is to implement a lambda proxy integration.
A proxy integration in API Gateway tells API Gateway to simply forward all headers to the integration for processing, which means you will see all of those values in your lambda function.
NOTE: Lambda has to return a specific response format back to API Gateway if it's a proxy integration. Ultimately, the response should be something like:
{
'statusCode': 200,
'headers': {
'echo-proxy': True
},
'body': 'some payload'
}
What you are trying to do right now is map everything manually, which is a deprecated approach and usually you don't want to do that unless you absolutely have to because it's kind of a pain.
If you have to map the headers manually start by mapping them in the the method request so it can carry on to the next step and so on. Basically like this:
Method Request -> Maps variable to Integration Request -> Maps variable to Body Mapping Template -> Maps variable to actual request header
What you have in your screenshot for the Integration Request -> HTTP Headers is:
Name: cokers
Mapped from: 'blah'
However, "mapped from" should look something like "method.request.header.coker" which is a standardized path (meaning to get the value from the Method Request Header field with name "coker").
Once you have added the coker header to the Method Request, and the Integration Request HTTP Headers are mapped correctly, you have to implement a mapping template. Set the content-type to application/json with passthrough set to "When there are no templates defined(recommended)" and a simple mapping template:
{
"headers": {
"coker" : "$input.params('coker')"
}
}
That is the way my API is setup and it returns the following to me because I had my lambda function return the event as a json object back to API GW:
{"body": "{\"headers\": {\"coker\": \"mapped\"}}", "statusCode": 200}
NOTE: the value of my header "coker" in the request on the client side is "mapped"
UPDATED ANSWER
To map the original "coker" header to "coker2" (or any other name you want to give it) you simply set the name of the header in your mapping template like so:
{
"headers": {
"coker2" : "$input.params('coker')"
}
}
Then edit your lambda function to return "coker2" header and you should get a response like this:
{"body": "{\"headers\": {\"coker2\": \"mapped\"}}", "statusCode": 200}

How do I respond with a request header in AWS API Gateway

I have a POST request coming in with a header that I need to play back with the response (Example: "Validation: 123").
The integration returns synchronously, immediately.
The header is entirely non-functional and so doesn't need to be passed through to my integration. It just needs to be passed back with the response to the inbound request.
I am trying to do this by mapping the headers from the method request through to the method response, or via the integration request/response.
For example, I'd like to set the header mapping for the integration response to method.request.header.Validation (ie, identically to the integration request mapping). However, this is disallowed.
Is this the right approach?
This is a limitation of AWS API Gateway.

Can you statically set a Header and it's value when setting up an HTTP Proxy using AWS Api Gateway endpoint?

I am creating an http proxy using AWS Api Gateway. I would like to hard code some of the headers and their values to be forwarded as part of the request. I thought this might be possible in the 'Integration Request' portion of the proxy setup, but I can't seem to figure it out.
I'm trying to pass an Authorization header with an oauth key. I don't want to share this key with clients that have access to this service, since I will only provide a subset of access to users of this specific endpoint.
In the Integration Request, you can configure a static header value to be sent to the integration endpoint by putting the value inside of single quotes, e.g. 'my_static_header_value'.
Is it a problem to put those hardcoded headers in the request body ? It not, you could just use a template (in the integration request screen) :
{
"hardcoded_header": "$input.params('hardcoded_header')"
}
Hope this helps.