我试图做一个http get()通过传递某些请求values中headers,现在我更换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)将被上述标头替换。