1

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.

enter image description here

 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"
            ]
        }
    ]
}

2 Answers 2

1

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 })

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

2 Comments

I tried to add simplejson.dumps(info) to turn the result into a string, but I get back "list": "[{\"id\": 8000048, \"name\": \"ESTACAO DE SERVICO TETE\", \"addr This suggests that the dictionary is double compressed. It's a list of json dictionaries. So now how would I compress this list of dictionaries into a string that API Gateway would accept as a response?
@GriffinMaree You'll need to show more code, as that should not happen. For one thing in the code posted in your question, info does not contain a "list" property, so the structure you are stringifying isn't the same as in your example. What you need to do is stringify only the content of the body, not the content of list. For example, where info = [{ a: 1, b: 2 }], then body: JSON.stringify(info) will return a response of [{"a":1,"b":2}], and body: JSON.stringify({ list: info }) will return {"list":[{"a":1,"b":2}]}
0

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.

Comments

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.