Submission-created.js response body

Hi,

I’m pretty new to Netlify functions and am hoping to be pointed in the right direction.

I’m using Gatsby and have set up a submission-created.js file for custom form handling. I’m running into an issue with my response body.

I’ve gotten everything to work great. The CAPTCHA validates, the form submits successfully, and the custom handling works as expected. Where I’m running into an issue is when trying to work with the response.

Here is a quick idea of my set up:

onSubmit:

try {
  const response = await fetch("/", {

    ...fetch options...

  })

  const data = await response.json()

  } catch (err) {

    ...error handling...

  }

submission-created.js:

exports.handler = async (event, context) => {
  const payload = JSON.parse(event.body).payload

  try {

    ...custom handling here...

    return {
      statusCode: 200,
      body: JSON.stringify({ firstName: payload.firstName }),
    }

  } catch (err) {

    ...error handling here...

  }
}

When I run response.json() on the fetch response I get this error:

Unexpected token < in JSON at position 0

The response is returning HTML instead of JSON. In this case, it’s returning the Netlify Thank You page.

When I run response.text() the error goes away and I get the HTML.

I’ve tried without success to return a JSON object. I apologize if this isn’t Netlify related and more of an AWS lambda thing (new to lambda’s too).

Any ideas why this is happening? I’d like to continue relying on the Netlify form handling for things like validation rather than just creating my own custom function.

Thanks!

Hmm, outside of functions, seeing our html instead of your content means you’ve tried to load a file that was missing (we show our thank you page if you don’t have one where the POST is sent, for instance). I’m not a functions expert or much good at JS, but hopefully that will point you in the right direction! If not, let me know and my function-savvier teammates can take a look when they’re back at the keys on Thursday.