Error decoding lambda response: json / deploying a function

I have an issue when I try and deploy my Netlify functions to live, I receive this error message (although not when run locally).

error decoding lambda response: json: cannot unmarshal bool into Go value of type string.

Is this linked to returning:
exports.retData = oBody => {
console.log(‘retData’, oBody)
return {
statusCode: 200,
headers: {
“Access-Control-Allow-Origin”: “*”,
“Access-Control-Allow-Methods”: “GET,POST,DELETE”,
“Access-Control-Allow-Headers”:
“Access-Control-Allow-Headers, Origin, Accept, Authorization, X-Requested-With, Content-Type, Access-Control-Request-Method, Access-Control-Request-Headers”
},
body: JSON.stringify(oBody)
};
};

At a guess, oBody is a bare true or false (whether on purpose as designed or due to a fault), but the thing that parses your return object (presumably something written in Go?) expects body to be a string (which is not unreasonable for an HTTP response).

1 Like

Yes, @alexrussell, that’s correct. @BrianSHenderson, that error from our system is typically seen when the body you are returning is not a valid stringified JSON string. Can you share what the value of oBody is? It’ll need to be a JSON object and not just a boolean as alex also suspects.

Edit: solved! This stackoverflow answer was the key: Revisions to Netlify NodeJS Function always returns 'Response to preflight request doesn't pass' - Stack Overflow

Summary: headers must be strings. I had:

'Access-Control-Allow-Credentials': true

It should have been:

'Access-Control-Allow-Credentials': 'true'

@Netlify this could really use some documentation.


I’m experiencing the same problem while returning a Stripe API response. I have no code written in GO at all.

The function works perfectly on local, but fails to return parsable JSON (or any plain text that looks like an object) when running on Netlify.

Logging on Netlify shows the function should be returning the following object, but it’s not:

{
  id: 'cs_live_foo_123,
  object: 'checkout.session',
  billing_address_collection: null,
  cancel_url: 'https://domain.com/pricing?status=cancel',
  client_reference_id: 'abc123',
  customer: null,
  customer_email: 'foo.bar@domain.com',
  livemode: true,
  locale: null,
  metadata: { planChosen: '{"plan":"lite","interval":"annual"}' },
  mode: 'subscription',
  payment_intent: null,
  payment_method_types: [ 'card' ],
  setup_intent: null,
  shipping: null,
  shipping_address_collection: null,
  submit_type: null,
  subscription: null,
  success_url: 'https://domain.com/checkout?status=true&newPlan=lite'
}

I have also tried returning a simpler object:

{ id: 'foo'}

Same result.

My Netlify function response function includes a JSON.stringify():

const respond = fulfillmentText => {
  console.log("about to stringify fullfillment text", fulfillmentText)
  return {
    statusCode: 200,
    body: JSON.stringify(fulfillmentText),
    headers: {
      'Access-Control-Allow-Credentials': true,
      'Access-Control-Allow-Origin': '*',
      'Content-Type': 'application/json',
    },
  }
}
1 Like

Thanks for sharing how you fixed this. We have seen other reports of this (Function started responding with 502s overnight), so I will file an issue for our Docs team and if there’s an update, we’ll report back here.

Update on this: we recently rolled out a change so that we’ll accept both integers and boolean values as header values :tada:

2 Likes