Angular/HttpInterceptor : Différence entre versions

De WikiSys
Aller à : navigation, rechercher
(Page créée avec « ==Intercepting requests and responses== * https://angular.io/guide/http#intercepting-requests-and-responses HTTP Interception is a major feature of '''@angular/common/htt... »)
(Aucune différence)

Version du 18 juillet 2019 à 16:05

Intercepting requests and responses

HTTP Interception is a major feature of @angular/common/http.

With interception, you declare interceptors that inspect and transform HTTP requests from your application to the server.

The same interceptors may also inspect and transform the server's responses on their way back to the application.

Multiple interceptors form a forward-and-backward chain of request/response handlers.

Interceptors can perform a variety of implicit tasks, from authentication to logging, in a routine, standard way, for every HTTP request/response.

Without interception, developers would have to implement these tasks explicitly for each HttpClient method call.

Write an interceptor
import { Injectable } from '@angular/core';
import { HttpEvent, HttpInterceptor, HttpHandler, HttpRequest} from '@angular/common/http';
import { Observable } from 'rxjs';

/** Pass untouched request through to the next request handler. */
@Injectable()

export class NoopInterceptor implements HttpInterceptor {

  intercept(req: HttpRequest<any>, next: HttpHandler):
    Observable<HttpEvent<any>> {
    return next.handle(req);
  }
}