Requests to API Gateway fail with CORS error if I add headers.

0

I have an API gateway which is integrated with Lambda. It was working fine so far while accessing from my localhost, but after adding Authorization (or any other) headers to the request, I get the following errors Access to XMLHttpRequest at 'https://******.amazonaws.com/prod/api/chat?id=****' from origin 'http://localhost:8080' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: It does not have HTTP ok status. Enter image description here

Here is the code snippet

const apiResponse = await axios.post(
          `https://******.amazonaws.com/prod/api/chat?id=${this.$route.params.p}`,
          response.data,
          {
            headers: {
              'Authorization': `Bearer some-token-here`,
            }
          }
        )

I believe my CORS configuration should allow headers through, but it doesn't work. Enter image description here Enter image description here Enter image description here Enter image description here

Please advise

  • The screenshots are too small / not readable to review if Access-Control-Allow-Headers includes "Authorization". Can you add those as text into the question?

vc2301
已提问 2 个月前366 查看次数
2 回答
1

Hello,

The CORS error you're experiencing when adding headers is likely due to the configuration on AWS API Gateway not matching the requirements of your application's requests. Ensure that:

  1. CORS is enabled on your API Gateway.
  2. The Lambda function returns the necessary CORS headers if you're using Lambda Proxy Integration.
  3. The OPTIONS preflight response is correctly configured in API Gateway to include CORS headers.

After making any changes, redeploy your API for the updates to take effect. If the issue persists, verify the request headers and consult CloudWatch Logs for further debugging.

profile picture
已回答 2 个月前
profile picture
专家
已审核 1 个月前
1

This error might be occurring because your API Gateway is not configured to handle CORS for requests with custom headers like Authorization. When you add custom headers to your request, the browser sends a preflight request (an HTTP OPTIONS request) to the server to check if the actual request is allowed. Your API Gateway needs to respond to this preflight request with the appropriate CORS headers.

Solution: Enabling CORS for a REST API resource

profile picture
专家
已回答 1 个月前

您未登录。 登录 发布回答。

一个好的回答可以清楚地解答问题和提供建设性反馈,并能促进提问者的职业发展。

回答问题的准则