Successful proxy response blocked by lambda error

Client → Netlify function (as proxy) → API

The code works, the conctact form emails are sent and 200 response + json success message returned to the netlify function. I can log the response data both as text() and json() in the netlify console, it looks normal there.

But the Netlify function refuses processing the response. Reduced down to this non-working example:

import fetch from 'node-fetch'
const url = 'https://mybackend.com/api/contact/'

const fetchHeaders = {
  'Access-Control-Allow-Origin': '*',
  'Accept': 'application/json',
  'Content-Type': 'application/json',
 'Host': 'mybackend.com'
}

exports.handler = async function (event, context) {

    const response = await fetch(url, {
      method: 'POST',
      body: event.body,
      headers: fetchHeaders
    })

    const data = await response.json()
   // const data = await response.text()

    return {
      status: 200,
      body: JSON.stringify(data)
     // body: data
    }
}

In any case the error displayed in the browser network tab is “error decoding lambda response: invalid status code returned from lambda: 0”

I appreciate any help. The information out there on this issue is very limited.

Hi @elhansson

Try changing status: 200 to statusCode: 200

return {
      statusCode: 200,
      body: JSON.stringify(data)
     // body: data
    }
1 Like

Yay, that was it. So weird, I have this exact same code in production for another contact form and it works even though I return status: and not statusCode. I had no idea there was only certain key names that could be returned, is this something new?

I don’t believe statusCode is new. All functions playground examples use it as well as every function I have seen.

@coelmay You’re right, I missed that the function I have working is using statusCode and not status. Thanks a lot for your time.