简体   繁体   中英

AWS API Gateway api returns request headers failed error

I am calling API using react.js application. I am getting error from AWS API Gateway about CORS policy:

Access to XMLHttpRequest at 'https://awsapigatewayid.execute-api.ap-south-1.amazonaws.com/staging/top10' from origin 'http://localhost:3000' has been blocked by CORS policy: Request header field access-control-allow-origin is not allowed by Access-Control-Allow-Headers in preflight response.

From React App code for call api like:

let options = {
    url: `https://awsapigatewayid.execute-api.ap-south-1.amazonaws.com/staging/top10`,
    method: 'POST',
    headers: {
      'Accept': 'application/json',
      'Content-Type': 'application/json',
      'X-Req-Time': getCurrentTimestamp(),
      'Access-Control-Allow-Origin': '*',
    },
    data: data || {},
  };
  if (isAuthorised) {
    options.headers['X-Auth-Token'] = 'usertokengoeshere';
  }
  const response = await axios(options);

My API from AWS API Gateway:

在此处输入图像描述

You need to remove the 'Access-Control-Allow-Origin': '*' , from your API call, to solve your error, instead add it to your Lambda response. Preferably not with '*' but with your actual Origin.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM