1
votes

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? - jellycsc

1 Answers

0
votes

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.