'API Gateway HTTP API CORS

I am using the new API Gateway HTTP which during the configuration enables you to add CORS. So I have set the Access-Control-Allow-Origin Header with the setting *.

However when I make a request using Postman I do not see that header and this i causing my VueJS Axios request to fail.

I previously used a Lambda Proxy Integration and did the following in my Lambda

"headers": { 
            "Access-Control-Allow-Origin": "*" 
        }

However the new HTTP API just does not seem to implement CORS. Maybe I am missing something simple.

--EDITS--

So I have continue to find an answer and came across a blog post from the guys at Serverless who set the following

It’ll ensure following headers:

Access-Control-Allow-Origin: *
Access-Control-Allow-Headers:

Content-Type, X-Amz-Date, Authorization, X-Api-Key, X-Amz-Security-Token, X-Amz-User-Agent
Access-Control-Allow-Methods:

OPTIONS, and all the methods defined in your routes (GET, POST, etc.)

I have tried these and redeployed and still only get the standard headers

Thanks



Solution 1:[1]

For anyone using HTTP API and the proxy route ANY /{proxy+}

You will need to explicitly define your route methods in order for CORS to work.

enter image description here

Wish this was more explicit in the AWS Docs for Configuring CORS for an HTTP API

Was on a 2 hour call with AWS Support and they looped in one of their senior HTTP API developers, who made this recommendation.

Hopefully this post can save some people some time and effort.

Solution 2:[2]

If you have a JWT authorizer and your route accepts ANY requests, your authorizer will reject the OPTIONS request as it doesn't contain an Authorization/Bearer token. To resolve this issue, you need to explicitly point your route to the HTTP request/method you need. E.g. POST

In that case, your authorizer will ignore the OPTIONS request without a JWT and proceed with the required request.

Solution 3:[3]

(This is answer just for AWS HTTP api gateway/AWS api gateway v2). I have met the same problem and I asked the AWS support finally. The tricky thing is actually CORS had been already working after we configured it, but I just didn't get how to test it (it's different from AWS REST API ), when test we need to specify the Origin(should be same with your setting:Access-Control-Allow-Origin in cors, like: http://example.com) and the Request method. For example:

curl -v -X GET https://www.xxx.yy/foo/bar/  -H 'Origin:http://example.com' -H 'Access-Control-Request-Method: GET'

Then it would return the CORS header you had set in response header: access-control-allow-origin, access-control-allow-methods, access-control-allow-headers, etc.

I just hope this can save time for who met the similar problem. By the way, I hope AWS can update this test way to offical Document(it's not very useful, but most of people must be saw it before find real answer): https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-cors.html

Solution 4:[4]

So even though i was getting cors headers in my post call, browsers were still failing,,

My solution was

  • explicitly create an OPTIONS route with same path {proxy+}
  • attach same lambda integration to it
  • have the lambda function return early with success headers
  if (method === 'OPTIONS') {
    return {
      headers: { 'Access-Control-Allow-Origin': '*' },
      statusCode: 200
    }
  }

Solution 5:[5]

tldr; x-api-key

It took some searching and trial and error, but I got CORS working for API Gateway HTTP API. For the very basic GET request from a jQuery ajax call here is what I had to do:

AWS Console CORS settings, needed Access-Control-Allow-Headers: x-api-key

enter image description here

Then, in my ajax call, I had to send a dummy x-api-key (I did not configure one, so not sure why it wants it.):

$.ajax ({
        url: 'https://xxxxxxx.execute-api.us-east-1.amazonaws.com/prod/stuff/',
        crossDomain: true,
        method: 'GET',
        headers: {
              "accept": "application/json",
              "X-Api-Key":"blablablalla"
          },
        success:function(data){
            doStuff(data);
        },
        error: function(){
            alert('error');
        }
    })

You may need additional configuration depending on your situation, but the x-api-key was what I narrowed down as the oddest undocumented requirement.

Solution 6:[6]

My issue was that I was sending this headers :
Access-Control-Allow-Origin
Access-Control-Allow-Methods
Access-Control-Allow-Credentials
From the web request and it was messing with the Access-Control-Allow-Headers. I removed them and it's fine now.

To debug CORS issue with AWS HTTP API, try to put a * in each field of the CORS configuration in the AWS Console and reconfigure each field one by one.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 TrieuNomad
Solution 2 AlexisP
Solution 3
Solution 4 metaory
Solution 5 Erik Pearson
Solution 6 Quentin Del