通过设置withCredentials为true来发送HttpOnly Cookie。
下面是一个示例:
import { Injectable } from '@angular/core'; import { HttpClient, HttpHeaders } from '@angular/common/http';
@Injectable({ providedIn: 'root' }) export class ApiService {
private endpoint = 'https://example.com/api/';
constructor( private http: HttpClient ) {}
public getUser(token: string) {
const headers = new HttpHeaders({
Authorization: Bearer ${token}
});
return this.http.get(
`${this.endpoint}user`,
{ withCredentials: true, headers }
);
} }