Interceptor Angular 4.3 - Set multiple headers on the cloned request

后端 未结 4 1710
夕颜
夕颜 2020-12-16 10:47

I just noticed that the Header Object that was possible to use in the previous HTTP RequestsOption is not anymore supported in the new Interceptor.

It\'s the new Int

4条回答
  •  一个人的身影
    2020-12-16 11:30

    Angular 4.3+

    Set multi headers in Interceptor:

    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, next: HttpHandler): Observable> {
    
        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);
      }
    }
    

提交回复
热议问题