502 Gateway Error for a netlify function

So when I am triggering a Netlify function from client side I am geting the following error

So i suspected it might be due preflight request issue. So I have added the OPTIONS method as shown below.

So OPTIONS method does return ‘Access-Control-Allow-Origin’: '*'

Netlify function when triggered from a REST Client does return ‘Access-Control-Allow-Origin’: ‘*’ in the response headers.

But when triggering from client side , I am getting the aforementioned error. Why am i getting the 502 Bad Gateway error from Netlify ?

@fool Could you provide your insights ?

Hi @titusrobyk,

I think you are trying to reject the OPTION request based on your code. I’m not sure why exactly you are getting a 502 but you probably will want to response for a 20x type status for OPTION requests since that is required for CORS to work correctly. Can you try just return a 200 and see if that works better? Otherwise, I don’t think a 405 response would work. Let me know how it goes.