小编SGR*_*SGR的帖子

如何以角度向 httpRequest 添加自定义标头

我试图做一个http get()通过传递某些请求valuesheaders,现在我更换headers这样的:

import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Injectable } from '@angular/core';
import {ICustomer} from 'src/app/models/app-models';

@Injectable({
  providedIn: 'root'
})
export class MyService {
private baseUrl = '....api url....';
public authKey = '....auth_key......';

  constructor(private http: HttpClient) { }

  public async AllCustomers(): Promise<ICustomer[]> {
   const apiUrl = `${this.baseUrl}/customers`;

   return this.http.get<ICustomer[]>(apiUrl ,
    {headers: new HttpHeaders({Authorization: this.authKey})}).toPromise();<=====
  }

}
Run Code Online (Sandbox Code Playgroud)

当我像这样替换标题时:

headers: new HttpHeaders({Authorization: this.authKey})

默认标头值(即 Content-Type : application/json)将被上述标头替换。

在此处输入图片说明 我没有替换标题如何添加custom headers,而是像这样尝试: …

javascript node.js rxjs typescript angular

2
推荐指数
1
解决办法
8294
查看次数

标签 统计

angular ×1

javascript ×1

node.js ×1

rxjs ×1

typescript ×1