aws-serverless-webapp-workshop icon indicating copy to clipboard operation
aws-serverless-webapp-workshop copied to clipboard

CORS not getting enabled on POST via API Gateway

Open shikhar1987 opened this issue 2 years ago • 0 comments

Enabling cors via API gateway doesn't return the required headers. Looks like you cannot enable CORS via API gateway if you've configured lambda as proxy integration. This is mentioned here: https://docs.aws.amazon.com/apigateway/latest/developerguide/how-to-cors.html#how-to-cors-consolelf.

Had to manually update the lambda code to include the following:

                "Access-Control-Allow-Headers" : "Content-Type",
                "Access-Control-Allow-Methods": "OPTIONS,POST,GET"

Here's the place where you should modify in case you run into a similar issue:

        callback(null, {
            statusCode: 201,
            body: JSON.stringify({
                RideId: rideId,
                Unicorn: unicorn,
                UnicornName: unicorn.Name,
                Eta: '30 seconds',
                Rider: username,
            }),
            headers: {
                'Access-Control-Allow-Origin': '*',
                "Access-Control-Allow-Headers" : "Content-Type",
                "Access-Control-Allow-Methods": "OPTIONS,POST,GET"
            },
        });

This now makes the unicorn pop up in my requested location.

shikhar1987 avatar Aug 23 '22 18:08 shikhar1987