AWS Lambda with API Gateway: CORS Headers Not Being Set for Preflight Requests
I'm learning this framework and I'm relatively new to this, so bear with me. I'm trying to set up an AWS Lambda function behind API Gateway, and I need to handle CORS properly for my frontend application. While my GET requests work fine, I'm having issues with preflight requests. When I send a POST request from my frontend, I get a `CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource` behavior. I've configured CORS in API Gateway, but it still seems to be failing for preflight OPTIONS requests. My Lambda function does return the appropriate headers, but the question continues. I've added the following code to my Lambda function to set the headers: ```javascript exports.handler = async (event) => { const response = { statusCode: 200, headers: { 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Methods': 'OPTIONS,POST,GET', 'Access-Control-Allow-Headers': 'Content-Type', }, body: JSON.stringify({ message: 'Success' }), }; return response; }; ``` In the API Gateway, I have enabled CORS on the method level and set the following response headers: - Access-Control-Allow-Origin: '*' - Access-Control-Allow-Methods: OPTIONS, POST, GET - Access-Control-Allow-Headers: Content-Type Despite this, when I test the POST request in the browser, it still triggers a preflight request (OPTIONS), which fails with the CORS behavior. I’ve also checked the logs in CloudWatch, and it seems that the OPTIONS request is not hitting my Lambda function at all. I’ve tried redeploying the API and double-checked all configurations, but nothing seems to resolve the scenario. Is there something I’m missing in how to configure API Gateway for CORS with Lambda? Any insights would be greatly appreciated! For context: I'm using Javascript on Ubuntu 20.04. I'd be grateful for any help.