API Gateway (500 Internal Server Error)

I have exhausted every other avenue and hoping someone on this forum can offer some insight. I have searched through the forum and none of the other “internal server error” issues contain information that has helped me.

My API is built with API gateway and I have an endpoint for a webhook for a third party service I’m using. For some reason or another, API gateway is returning a 500 internal server error. What I cannot understand, is that the lambda executes, performs by DB update as it’s supposed to, but when it comes time to send the response, it’s sending 500 instead of the desired 200 status code.

From my serverless.yml:

  snapWebhook:
    handler: src/handler.snapWebhook
    events:
      - http:
          method: POST
          path: webhooks/snap
          private: false # Requires clients to add API keys values in the `x-api-key` header of their request
          cors: true

The webhook request comes in as application/x-www-form-urlencoded, here is the handler:

// application/x-www-form-urlencoded
const webhook = middy((event, context, callback) => {
  console.log(event);
  const params = qs.parse(event.body);
  const payload = params["payload"];
  var decoded = decodeURIComponent(payload);
  console.log({ decoded });
  console.log(JSON.parse(decoded));
  const payloadObj = JSON.parse(decoded);
  handleWebhook(payloadObj)
    .then((result) => {
      console.log({ result });
      console.log("success!");
      let res = {
        statusCode: 200,
        body: JSON.stringify({ message: "success" }),
      };
      callback(null, res);
    })
    .catch(callback);
});

As I mentioned, the lambda executes and performs the desired actions inside the handleWebhook fn. I can see it succeed in the Cloudwatch logs:

2020-06-29T12:05:28.721-04:00

2020-06-29T16:05:28.721Z 94f18198-8dd0-470e-b31e-e099808309a1 INFO { body: 'payload=%257B%2522id%2522%253A1247%252C%2522status%2522%253A%2522completed%2522%252C%2522totalAmount%2522%253A10800%252C%2522tipAmount%2522%253A0%252C%2522feeAmount%2522%253A373%252C%2522settleAmount%2522%253A10427%252C%2522requiredAmount%2522%253A10800%252C%2522date%2522%253A%25222020-06-29T15%253A27%253A49Z%2522%252C%2522snapCode%2522%253A%25228J9z9lVZ%2522%252C%2522snapCodeReference%2522%253A%2522d4cdca8d-b6ad-468b-afa4-c51ad74ce7a2%2522%252C%2522userReference%2522%253A%2522Both3129%2522%252C%2522merchantReference%2522%253A%2522BOTH3129-06-29%2522%252C%2522statementReference%2522%253Anull%252C%2522authCode%2522%253A%2522950620%2522%252C%2522deliveryAddress%2522%253Anull%252C%2522extra%2522%253A%257B%2522amount%2522%253A%252210800%2522%252C%2522id%2522%253A%2522BOTH3129-06-29%2522%252C%2522orderId%2522%253A%25223129ab49-f2be-422b-85b7-48d2bb24bc91%2522%252C%2522updated%2522%253A%2522true%2522%252C%2522strict%2522%253A%2522true%2522%257D%252C%2522isVoucher%2522%253Anull%252C%2522isVoucherRedemption%2522%253Afalse%257D' }

...

2020-06-29T16:05:29.163Z 94f18198-8dd0-470e-b31e-e099808309a1 INFO { result: { msg: 'success' } }

2020-06-29T12:05:29.164-04:00

2020-06-29T16:05:29.164Z 94f18198-8dd0-470e-b31e-e099808309a1 INFO success!

At which it should return 200, but instead I get the 500 error. I have tried a multitude of different ways of formatting the response, but no luck. The lambda has the correct permissions for API gateway and viceversa.

The only clue I have is when using an API client to make the request, the headers indicate that there’s a cloudfront error:

In case it’s relevant, I have the route in API gateway configured LAMBDA_PROXY:

Any help would be greatly appreciated!

Hi @Craig did you manage to resolve this? we are also facing same issue.