我正在用 Angular 构建一个 Web 应用程序。我正在尝试向 REST API 发送 POST 请求,但收到 403。如果我在 cmd 中发送curl 请求,它工作得很好。
这是 TypeScript 中的代码:
let header=new HttpHeaders().set('accept','application/json')
header=header.set('X-AbiliSense-API-Key','18306df7-3fde-49a1-9bef-6ad7a9d83e7f')
this.NewPost=this.http.post('https://api.abilisense.com/v1/api/registerDevice',{header:header});
在命令中:
curl -X POST "https://api.abilisense.com/v1/api/registerDevice" -H "accept: application/json" -H "X-AbiliSense-API-Key: 18306df7-3fde-49a1-9bef-6ad7a9d83e7f"//this works
最佳答案
如@Jacob explained ,post()
的第二个参数是请求正文,而 header 必须在第三个参数中传递。
添加null
作为第二个参数,并在第三个参数中使用headers
(而不是header
):
let headers = ...
this.NewPost = this.http.post('https://api.abilisense.com/v1/api/registerDevice', null, {headers: headers});
关于javascript - AngularJS 中的 HTTP POST 请求返回 403,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60955965/