aws-samples / aws-serverless-webapp-workshop

Code and walkthrough labs to set up serverless applications for Wild Rydes workshops
https://serverlessland.com
Other
56 stars 144 forks source link

CORS not getting enabled on POST via API Gateway #33

Open shikhar1987 opened 1 year ago

shikhar1987 commented 1 year ago

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.