首页 > 解决方案 > 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
})

当我 console.log 请求选项时: 在此处输入图像描述

我如何重组事物以便在我的请求中识别我的标头的完整值?任何信息都会有所帮助!谢谢你

ps 我以前使用的是 Rails API,所以为了连接,我会使用 Angular2Token 模块和“cors”gem 来发出请求,所以我试图摆脱它。

标签: angularapihttp

解决方案


推荐阅读