angular - Http请求标头无法正常工作,Angular 5
问题描述
我正在构建一个 Angular 5 应用程序,我正在向外部服务器发出 api 请求。我正在尝试对一些标头进行硬编码,包括内容类型和“x-api-key”。在邮递员中,请求完美运行,所以我知道我有正确的信息。
问题是我将向请求添加标头,当我 console.log 请求选项时,它会将它们显示在一个数组中,但是在网络选项卡中检查它时,它只会显示我包含的标头的标题( content-type 和 x-api-key) 而不是值本身。我收到 403/forbidden 错误,所以我知道 API 可以识别请求,但我无权获取信息。见下文 -
这是我添加标题的方式:
auth.service.ts
createAuthorizationHeader(headers: Headers) {
headers.append('Authorization', 'Basic ' + btoa('username:password'));
headers.append('x-api-key', '...');
headers.append('Content-type', 'application/json')
}
然后我在哪里调用 api-
logInUser(signInData: { username: string, password: string }): Observable<Response> {
let headers2 = new Headers();
this.createAuthorizationHeader(headers2);
return this.authService.post('http://api_url.com', signInData,{
headers: headers2
})
我如何重组事物以便在我的请求中识别我的标头的完整值?任何信息都会有所帮助!谢谢你
ps 我以前使用的是 Rails API,所以为了连接,我会使用 Angular2Token 模块和“cors”gem 来发出请求,所以我试图摆脱它。
解决方案
推荐阅读
- dart - 使用 dart LinkedList 键入擦除?
- python - 正则表达式:如何将重复块作为组()?
- python - Selenium 在网页中找不到元素
- snowflake-cloud-data-platform - 雪花数据到 OnPrem Tableau?传输/移动中的数据是否加密?
- excel - 为什么 Excel 中的以下日期比较返回 false?
- python - 通过python创建时间表
- assembly - 两种不同的乘法结果
- javascript - React 组件不使用上下文 API 从全局状态呈现
- android - 如果我的 google 登录不是由 firebase 管理,我是否需要 SHA1?
- python-3.x - Pytorch 无法将训练好的模型导出为 ONNX