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',
},
}
}