【发布时间】:2019-11-08 00:57:39
【问题描述】:
我正在尝试通过在headers 中传递一些values 来发出http get() 请求,现在我将像这样替换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})
默认的 headers 值(即 Content-Type : application/json)将被上面的 headers 替换。
我没有替换标题如何添加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 的新手,有什么帮助吗?
【问题讨论】:
-
如果你想发送身份验证令牌,为什么不使用 http 拦截器?
标签: javascript node.js angular typescript rxjs