我正在尝试使用 Angular 中的 http。我的代码如下所示:
const url ='this is my url';
const headers = new Headers({'Authorization': 'this is my token'});
return this.http.put(url, {headers: headers}).toPromise().then......
但我一直收到 401 Unauthorized 作为请求状态代码。 我尝试将我的请求从 Chrome 网络选项卡复制到 Postman,我注意到授权 header 已添加到请求正文,而不是 header 。
这正常吗?
如果我在 Postman 中手动添加授权 header 作为 header ,请求将按预期工作。
最佳答案
根据文档 https://angular.io/api/http/Http
Http.put 方法签名是:
put(url: string, body: any, options?: RequestOptionsArgs): Observable<Response>
因此,您的第二个参数应该是数据/正文,而不是选项。
尝试:
return this.http.put(url, {}, {headers: headers}).toPromise().then......
关于Angular2 HTTP put 添加授权 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49171566/