简体   繁体   English

使用带有自定义 header 的 fetch() 时获取 401 错误代码

[英]Getting 401 Error code while using fetch() with a custom header

=== ===

I've built a custom API with AWS API Gateway.我已经使用 AWS API 网关构建了自定义 API。
For one of the method, I've enable the authorization to be checked using a Lambda function.对于其中一种方法,我启用了使用 Lambda function 检查授权。
In order to make it work, I have to add the following key: Key: authorizationToken Value: allow .为了让它工作,我必须添加以下键: Key: authorizationToken Value: allow

I've tested it using Postman and it's working fine, my POST is processed and I receive a response.我已经使用 Postman 对其进行了测试,它工作正常,我的 POST 已处理并且我收到了回复。

I'm just starting with Javascript so I've used the code provided in Postman. Here it is:我刚从 Javascript 开始,所以我使用了 Postman 中提供的代码。它是:

function getData(event){
    var myHeaders = new Headers();
    myHeaders.set("authorizationToken", "allow");

    var requestOptions = {
      method: 'POST',
      mode: 'no-cors'
    };

    fetch("https://[THE_URL_OF_MY_API]/prod/counter", requestOptions)
      .then(response => response.text())
      .then(result => console.log(result))
      .catch(error => console.log('error', error));
}

And i'm getting the following error message in the console.我在控制台中收到以下错误消息。

script.js:49 POST https://[THE_URL_OF_MY_API]/prod/counter 
net::ERR_ABORTED 401 (Unauthorized)
getData @ script.js:49

I've looked into the logs of the API Gateway in AWS in order to troubleshoot it:为了排除故障,我查看了 AWS 中 API 网关的日志:

  • But I can't see any logs so it seems my fetch is being block before it's even being sent.但是我看不到任何日志,所以我的提取似乎在发送之前就被阻止了。
  • I checked the headers of the successful API call sent by Postman and I can't find any header apart from mine and the one generated by the application automatically.我检查了由 Postman 发送的成功 API 呼叫的标头,除了我的和应用程序自动生成的以外,我找不到任何 header。

What am I doing wrong?我究竟做错了什么?

Note: I'm using similar code to another endpoint where the authorization is not enabled and it's working fine.注意:我正在使用与未启用授权但工作正常的另一个端点类似的代码。 SO I guess my header is not correctly set.所以我猜我的 header 设置不正确。

Thanks !谢谢 !

@CRice, Salmin Skenderovic, Jaromanda X: Thanks a lot for your feedback. @CRice、Salmin Skenderovic、Jaromanda X:非常感谢您的反馈。

The missing myHeaders was a typo, I fixed it.缺少的 myHeaders 是错字,我已将其修复。 Seeing the comment about the 'no-cors', I've looked into it, enable CORS, authorized my specific header in Access-Control-Allow-Headers.看到关于“no-cors”的评论,我查看了它,启用 CORS,在 Access-Control-Allow-Headers 中授权我的特定 header。

And now it's working fine.现在它工作正常。

My amended code:我修改后的代码:

    var myHeaders = new Headers();
    myHeaders.set("authorizationToken", "allow");

    var requestOptions = {
      method: 'POST',
      redirect: 'follow',
      headers : myHeaders
    };

    fetch("https://[URL_OF_MY_API_ENDPOINT]/prod/counter", requestOptions)
      .then(response => response.text())
      .then(result => console.log(result))
      .catch(error => console.log('error', error));

Configuration of my API Gateway: Configuration of my API Gateway我的API网关的配置:我的API网关的配置

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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