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 個月前

您尚未登入。 登入 去張貼答案。

一個好的回答可以清楚地回答問題並提供建設性的意見回饋,同時有助於提問者的專業成長。

回答問題指南