Laravel Sanctum鉴权:带Authorization头请求出现net::ERR_EMPTY_RESPONSE
Laravel Sanctum鉴权浏览器请求异常排查
我的API采用Laravel Sanctum实现鉴权,调用受auth:sanctum中间件管控的接口时遇到以下异常情况:
- 在**浏览器环境(基于Ionic+Angular开发的应用)**发起请求,只要添加
Authorization请求头,就会触发错误:GET http://myapi.host.com/api/user net::ERR_EMPTY_RESPONSE - 不携带
Authorization头请求时,接口能正常响应,但返回GET http://myapi.host.com/api/user 401 (Unauthorized),无法完成身份验证 - 用Postman调用该接口完全正常,初步排除服务器配置问题
带Authorization头的请求代码
const headers = new HttpHeaders({ Authorization: `Bearer ${await this.apiHelper.getToken()}` }); this.http .get(`${this.uri}/api/user`, { headers: headers, }) .subscribe( (res) => console.log(res), (error) => console.log(error) );
对应的错误信息:GET http://myapi.host.com/api/user net::ERR_EMPTY_RESPONSE
不带Authorization头的请求代码
this.http .get(`${this.uri}/api/user`) .subscribe( (res) => console.log(res), (error) => console.log(error) );
对应的响应:GET http://myapi.host.com/api/user 401 (Unauthorized)
内容的提问来源于stack exchange,提问作者Fran
相关产品推荐
相关产品推荐

