event.body in lambda function is not an object even though application/json header exists

Viewed 115

I have an API Gateway / lambda function set up with a LAMBDA_PROXY integration. I have a POST resource which is sending a JSON object in the body like the following:

{
    "version": 123,
    "attributes": [
        {
            "id": 1123,
            "type": "integer",
            "defaultValue": 88
        }
    ]
}

The POST request is sent with a Content-Type: application/json header. I expect the lambda function to receive event.body as an object so that I will be able to reference the object like this:

const version = event.body.version;

In fact, this does not work and I am forced to run a JSON.parse() on event.body. At first I was thinking that the content-type header was not getting to the lambda but then I printed to the log event.headers and the header is in fact there:

{
    "Accept": "*/*",
    "content-type": "application/json",
    "Host": "jfpvip409c.execute-api.eu-west-1.amazonaws.com",
    "User-Agent": "curl/7.65.0",
    "X-Amzn-Trace-Id": "Root=1-5fe9940a-3f3634ce4ccd26f5211c21d1",
    "X-Forwarded-For": "192.118.35.111",
    "X-Forwarded-Port": "443",
    "X-Forwarded-Proto": "https"
}

Can somebody please help me understand why this is the case ?

Thanks in advance
nsteiner

0 Answers
Related