Enable cors on api gateway

Im tryng to access my api gateway from a react app, but im getting cors error. I already tried adding the header on the nodejs code

like this

module.exports.fetchPokemon = async (event) => {
    try{
        let response = await getPokemon(event.pathParameters.name);
        return {
            statusCode: 200,
            headers: {
                "Access-Control-Allow-Origin" : "*", 
                'Content-Type': 'application/json', 
              }, body:response};
    }catch(error){
        return error;
    }

};

any idea?

Upvotes: 0

Views: 213

Answers (1)

Matus Dubrava
Matus Dubrava

Reputation: 14462

You can go to Management Console -> API Gateway. Click on the resource for which you want CORS to be enabled and click Actions -> Enable CORS and then Deploy.

Another option is to set the Access-Control headers via integration response -> header mappings which you will see when you click on particular method in your resources.

Also, you can't set Access-Control-Allow-Origin to * if you have set Access-Control-Allow-Credentials to true. What you will need to do in such case is to specify protocol, port and domain instead of *, or a list of them.

Upvotes: 1

Related Questions