SyntaxError - Unexpexted end of JSON input

Hello community,
This is my first post here, so forgive me if it lacks information.

I’m building a simple website and I’m using Lambda Functions to send the content of a form to Mailgun. I’ve tested locally and it works, but when I deploy I keep getting an error from the Lambda Functions.

  {
  "errorType": "SyntaxError",
  "errorMessage": "Unexpected end of JSON input",
  "trace": [
    "SyntaxError: Unexpected end of JSON input",
    "    at JSON.parse (<anonymous>)",
    "    at Runtime.n.handler (/var/task/src/test.js:1:1027)",
    "    at Runtime.handleOnce (/var/runtime/Runtime.js:66:25)"
  ]
}

I’ve tried creating a simple function just to debug the JSON.parse problem, and I don’t know what I’m doing wrong, since it again worked locally.

My test function file looks like this:

exports.handler = function(event, context, callback) {
  const data = JSON.parse(event.body)
  console.log(data.name)
  callback(null, {
    statusCode: 200,
    body: JSON.stringify(data.name),
  })
}

Did anybody have this problem too?
Thanks

I’m testing this function using Insomnia.

Hey @ffael,
It’s possible that event.body is not a JSON object. What happens when you console.log just event.body?

Hello Jen, thanks for replying!

When I test locally, it does log a JSON, but when I try in production I get the error.

what does your production function log output from line 3?

  console.log(data.name)

are you posting data via a form? is it possible to see that code?

Are you by any chance spinning up a different local server vs prod server, they have different data structures on their even returns. IE local lamda server, azure lambda, apollo server lamda.

Tldr, console log the event itself and see what you’re getting back.