拦截器 Angular 4.3 - 在克隆请求上设置多个 headers
Interceptor Angular 4.3 - Set multiple headers on the cloned request
我刚刚注意到新的拦截器不再支持以前的 HTTP RequestsOption 中可以使用的 Header Object。
这是new Interceptor逻辑:
// Get the auth header from the service.
const authHeader = this.auth.getAuthorizationHeader();
// Clone the request to add the new header.
const authReq = req.clone({headers: req.headers.set('Authorization', authHeader)});
现在,我有两种方法可以在此请求中添加我的 headers:
示例:
headers?: HttpHeaders;
headers: req.headers.set('token1', 'asd')
setHeaders?: {
[name: string]: string | string[];
};
setHeaders: {
'token1': 'asd',
'token2': 'lol'
}
如何根据此请求有条件地添加多个 headers?
与我以前对 Header Object:
所做的相同
myLovellyHeaders(headers: Headers) {
headers.set('token1', 'asd');
headers.set('token2', 'lol');
if (localStorage.getItem('token1')) {
headers.set('token3', 'gosh');
}
}
const headers = new Headers();
this.myLovellyHeaders(headers);
新的 HTTP 客户端使用不可变 headers object。您需要存储对前一个 headers 的引用以改变 object:
myLovellyHeaders(headers: Headers) {
let p = headers.set('token1', 'asd');
p = p.set('token2', 'lol');
if (localStorage.getItem('token1')) {
p = p.set('token3', 'gosh');
}
请参阅 Why HttpParams doesn't work in multiple line in angular 4.3 了解为什么需要存储对返回值的引用。
headers也是一样:
export class HttpHeaders {
...
set(name: string, value: string|string[]): HttpHeaders {
return this.clone({name, value, op: 's'});
}
private clone(update: Update): HttpHeaders {
const clone = new HttpHeaders();
clone.lazyInit =
(!!this.lazyInit && this.lazyInit instanceof HttpHeaders) ? this.lazyInit : this;
clone.lazyUpdate = (this.lazyUpdate || []).concat([update]);
return clone;
}
要了解有关拦截器背后机制的更多信息,请阅读:
Angular 4.3+
在拦截器中设置多个 headers:
import {
HttpEvent,
HttpInterceptor,
HttpHandler,
HttpRequest,
HttpHeaders
} from '@angular/common/http';
import {Observable} from 'rxjs/Observable';
import {environment} from '../../../../environments/environment';
export class SetHeaderInterceptor implements HttpInterceptor {
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
const headers = new HttpHeaders({
'Authorization': 'token 123',
'WEB-API-key': environment.webApiKey,
'Content-Type': 'application/json'
});
const cloneReq = req.clone({headers});
return next.handle(cloneReq);
}
}
我的代码使用以下方法添加新的 headers 以用新值替换以前的值:
headers: req.headers.set('token1', 'asd')
.set('content_type', 'asd')
.set('accept', 'asd')
要附加到克隆请求的现有 header(如在 HTTP 拦截器中),下面的代码有效(使用 Angular 5.x)。在下面的例子中,它附加到现有的 header(在我的例子中包括 Angular 自动包含的 XSRF-TOKEN cookie),带有存储在 sessionStorage 中的 JWT 授权令牌:
export class TokenInterceptor implements HttpInterceptor {
constructor() { }
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
let headers = request.headers
.set('Content-Type', 'application/json')
.set('Authorization', `Bearer ${sessionStorage.getItem('authToken')}`);
const cloneReq = request.clone({ headers });
return next.handle(cloneReq);
}
}
我刚刚注意到新的拦截器不再支持以前的 HTTP RequestsOption 中可以使用的 Header Object。
这是new Interceptor逻辑:
// Get the auth header from the service.
const authHeader = this.auth.getAuthorizationHeader();
// Clone the request to add the new header.
const authReq = req.clone({headers: req.headers.set('Authorization', authHeader)});
现在,我有两种方法可以在此请求中添加我的 headers:
示例:
headers?: HttpHeaders;
headers: req.headers.set('token1', 'asd')
setHeaders?: {
[name: string]: string | string[];
};
setHeaders: {
'token1': 'asd',
'token2': 'lol'
}
如何根据此请求有条件地添加多个 headers? 与我以前对 Header Object:
所做的相同 myLovellyHeaders(headers: Headers) {
headers.set('token1', 'asd');
headers.set('token2', 'lol');
if (localStorage.getItem('token1')) {
headers.set('token3', 'gosh');
}
}
const headers = new Headers();
this.myLovellyHeaders(headers);
新的 HTTP 客户端使用不可变 headers object。您需要存储对前一个 headers 的引用以改变 object:
myLovellyHeaders(headers: Headers) {
let p = headers.set('token1', 'asd');
p = p.set('token2', 'lol');
if (localStorage.getItem('token1')) {
p = p.set('token3', 'gosh');
}
请参阅 Why HttpParams doesn't work in multiple line in angular 4.3 了解为什么需要存储对返回值的引用。
headers也是一样:
export class HttpHeaders {
...
set(name: string, value: string|string[]): HttpHeaders {
return this.clone({name, value, op: 's'});
}
private clone(update: Update): HttpHeaders {
const clone = new HttpHeaders();
clone.lazyInit =
(!!this.lazyInit && this.lazyInit instanceof HttpHeaders) ? this.lazyInit : this;
clone.lazyUpdate = (this.lazyUpdate || []).concat([update]);
return clone;
}
要了解有关拦截器背后机制的更多信息,请阅读:
Angular 4.3+
在拦截器中设置多个 headers:
import {
HttpEvent,
HttpInterceptor,
HttpHandler,
HttpRequest,
HttpHeaders
} from '@angular/common/http';
import {Observable} from 'rxjs/Observable';
import {environment} from '../../../../environments/environment';
export class SetHeaderInterceptor implements HttpInterceptor {
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
const headers = new HttpHeaders({
'Authorization': 'token 123',
'WEB-API-key': environment.webApiKey,
'Content-Type': 'application/json'
});
const cloneReq = req.clone({headers});
return next.handle(cloneReq);
}
}
我的代码使用以下方法添加新的 headers 以用新值替换以前的值:
headers: req.headers.set('token1', 'asd')
.set('content_type', 'asd')
.set('accept', 'asd')
要附加到克隆请求的现有 header(如在 HTTP 拦截器中),下面的代码有效(使用 Angular 5.x)。在下面的例子中,它附加到现有的 header(在我的例子中包括 Angular 自动包含的 XSRF-TOKEN cookie),带有存储在 sessionStorage 中的 JWT 授权令牌:
export class TokenInterceptor implements HttpInterceptor {
constructor() { }
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
let headers = request.headers
.set('Content-Type', 'application/json')
.set('Authorization', `Bearer ${sessionStorage.getItem('authToken')}`);
const cloneReq = request.clone({ headers });
return next.handle(cloneReq);
}
}