Я пытаюсь сделать запрос 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();<=====
}
}
Когда я заменяю заголовки, подобные этому:
headers: new HttpHeaders({Authorization: this.authKey})
Значения заголовков по умолчанию (i, e Content-Type: application / json) будут заменены вышеуказанными заголовками.
Вместо того, чтобы заменить заголовки, как я могу добавить custom headers
, я попробовал так:
public async AllCustomers(): Promise<ICustomer[]> {
const apiUrl = `${this.baseUrl}/courses`;
const headers = new HttpHeaders();
headers.append('Authorization', this.authKey);
headers.append('x-Flatten', 'true');
headers.append('Content-Type', 'application/json');
return this.http.get<ICustomer[]>(apiUrl).toPromise();
}
Что не так с моим подходом, я новичок в angular
, Любая помощь?