[solved] Help, webhook works in dev but not at Netlify

this code works when I use postman to call my webhook but fails on Netlify

the Netlify log shows the last debug before the return recordHook payload:

but postman receives

error decoding lambda response: 
error decoding lambda response: 
json: cannot unmarshal object into Go struct field .body of type string

odd…

thanks, Al;

import type { APIGatewayEvent, Context } from 'aws-lambda'
import {
  verifyEvent,
  VerifyOptions,
  WebhookVerificationError,
} from '@redwoodjs/api/webhooks'
import { logger } from 'src/lib/logger'

import process from 'process'

export const handler = async (event: APIGatewayEvent, context: Context) => {
  const webhookInfo = { webhook: 'recordHook' }
  const webhookLogger = logger.child({ webhookInfo })

  webhookLogger.debug({ event: event }, 'recordHook event')

  try {
    // verifyEvent('skipVerifier', { event })

    const options = {
      signatureHeader: 'X-Webhook-Signature',
      issuer: 'repeater.dev',
    } as VerifyOptions

    webhookLogger.debug(
      `recordHook VerifyOptions: issuer: 'repeater.dev', secret: ${process.env.guid}`
    )
    verifyEvent('jwtVerifier', {
      event,
      secret: process.env.guid,
      options,
    })

    const payload = JSON.parse(event.body)
    webhookLogger.debug(
      `recordHook payload: ${JSON.stringify(payload, null, 2)}`
    )

    return {
      statusCode: 200,
      body: {
        now: Date.now(),
      },
    }
  } catch (error) {
    if (error instanceof WebhookVerificationError) {
      webhookLogger.warn('Unauthorized')

      return {
        statusCode: 401,
      }
    } else {
      webhookLogger.error({ error }, error.message)

      return {
        statusCode: 500,
        body: JSON.stringify({
          error: error.message,
        }),
      }
    }
  }
}

Hey @ajoslin103 looks like the content type on your success path is incorrect. It’s probably a good idea to be consistent and always respond with the same content type headers

e.g.

return {
    statusCode: 200,
    headers: {
      'Content-Type': 'application/json',
    },
   body: JSON.stringify({ now: Date.now()})
}

That being said, looks like we do have an inconsistency between the dev server and when you deploy to serverless, would you mind raising a github issue please? This is something we’ll look to handle when start normalising all serverless functions.

Ah I should update the examples here: Docs - Webhooks : RedwoodJS Docs to include

    headers: {
      'Content-Type': 'application/json',
    },

I’ll do that now. Thanks.

Edit: PR for this is here: docs: Function/Webhook examples use content-type header to avoid error decoding lambda response by dthyresson · Pull Request #935 · redwoodjs/redwoodjs.com · GitHub

Sadly, that did not solve the issue – no change in error message

You definitely need to return a string in the Body – you cannot return a date or anything that isn’t a string.

But, are you sure that you reach:

  const payload = JSON.parse(event.body)
    webhookLogger.debug(
      `recordHook payload: ${JSON.stringify(payload, null, 2)}`
    )

Is the JSON.parse failing? If the body is null, parse could fail.

Or is payload null?

A both Daniel and David said (and I missed) it does work when you return JSON.stringify(data)

body: JSON.stringify({
        now: Date.now(),
      }),