2

I have a python lambda function and an API Gateway Lambda Proxy integration.

The python code returns:

{
    "statusCode": str(code),
    "body": error if error else result.format(**event),
    "headers": {
        "Content-Type": "application/json",
        **kwargs,
    }
}

The response in the API Gateway looks like:

Endpoint response body before transformations: 
{
    "statusCode": "200",
    "body": "Some text.",
    "headers": {
        "Content-Type": "application/json",
        "Access-Control-Allow-Origin": "*",
        "Access-Control-Allow-Credentials": true,
        "kwarg": "foo"
    }
}

Mon May 25 20:18:44 UTC 2020 : Method response headers: {Content-Type=application/json, Access-Control-Allow-Origin=*, Access-Control-Allow-Credentials=true, password=1, X-Amzn-Trace-Id=Root=1-5ecc2824-c55ec6e1dc79ba1417361345;Sampled=0}

However, the response to the application loses all of the headers when it reaches the client. The client instead receives:

{
    "data":"Some text.",
    "status":200,
    "statusText":"",
    "headers":{
        "content-length":"10",
        "content-type":"application/json"
    },
    "config":{
        "method":"GET",
        "headers":{
            "Content-Type":"application/json",
            "Accept":"application/json"
        },
        "timeout":0,
        "transformRequest":[
            null
        ],
        "transformResponse":[
            null
        ],
        "url":"https://something.execute-api.region.amazonaws.com/test/page?foo=bar",
        "data":""
    }
}

What do I need to change?

Thanks in advance for your help.

1
  • Did you print kwargs and verify it in CloudWatch? Commented May 25, 2020 at 20:54

1 Answer 1

0

It turns out that a proxy request was not necessary and I needed the mapping template:

#set($allParams = $input.params())
{
"custom-parameters" : $input.json('$'),
"params" : {
#foreach($type in $allParams.keySet())
    #set($params = $allParams.get($type))
"$type" : {
    #foreach($paramName in $params.keySet())
    "$paramName" : "$util.escapeJavaScript($params.get($paramName))"
        #if($foreach.hasNext),#end
    #end
}
    #if($foreach.hasNext),#end
#end
},
"stage-variables" : {
#foreach($key in $stageVariables.keySet())
"$key" : "$util.escapeJavaScript($stageVariables.get($key))"
    #if($foreach.hasNext),#end
#end
}
}

on the response. Then I had to look in the result["data"]["custom-parameters"]["headers"] to find the kwargs. I hope this helps someone else with a similar problem.

Sign up to request clarification or add additional context in comments.

3 Comments

where exactly did you put this code to fix the issue - I have nodejs proxy lambda which is returning the headers but the UI/client code is not receiving it at all.
@AbdealiChandanwala have a look at docs.aws.amazon.com/apigateway/latest/developerguide/… for more information about mappings
Thanks George, I got my issue resolved, I was using a proxy lambda and was returning the required header's but later found that the variable was being undefined at runtime due to a syntax mistake.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.