Reputation: 61
There's probably a simple solution to this but I'm stuck. My lambda function being called by API Gateway should return my json response but it's giving me the same "Malformed proxy response" error
I've fiddled with the response a dozen times but as far as i can tell, there's nothing wrong with it? It's the same response as i'd get from a postman API. What am I doing wrong here?
The following screenshot is the response when testing in cloud9, but when fetching from API gateway using proxy response parameters, i get the "Malformed proxy response" error.
Response
{
"statusCode": 200,
"list": [
{
"id": 8000048,
"name": "ESTACAO DE SERVICO TETE",
"addr": "EN7, Bairro Samora Machel, Unidade Mpadue",
"lat": -16.195047,
"lon": 33.585467,
"amen": [
false,
false,
"",
false,
false,
false,
false,
false,
false,
"",
false,
false,
false,
false
],
"fuel": [
null,
null,
null,
null,
null
],
"loc": [
"Tete",
"Center",
"Mozambique",
"MZ"
]
}
]
}
Upvotes: 1
Views: 452
Reputation: 61
Sorry for the lack of info in this question, due to legal reasons I could not share everything. But the solution to this ended up being double compressing the json response. Let me explain ...
API gateway decompresses the response before returning it to the client. Due to this, responding to the event in lambda requires you to not only stringify the response but to kinda double stringify (or i call compress) the response. Basically, i had to ...
simplejson.dumps(response)
simplejson.dumps(response)
I'm not sure i understand this correctly or if I'm using the correct terms, so take all this with a pinch of salt. Thanks, everyone.
Upvotes: 0
Reputation: 8572
The response you're returning is invalid. As per the documentation,
In Lambda proxy integration, API Gateway requires the backend Lambda function to return output according to the following JSON format:
{ "isBase64Encoded": true|false, "statusCode": httpStatusCode, "headers": { "headerName": "headerValue", ... }, "multiValueHeaders": { "headerName": ["headerValue", "headerValue2", ...], ... }, "body": "..." }
Not all of those fields are required, but you can't just add random stuff to the response; rather you should set a stringified version of the object as the body
field, for example (using js as I don't know python) body: JSON.stringify(info)
or body: JSON.stringify({ list: info })
Upvotes: 1