{ "id": "api/common/http/HttpInterceptor", "title": "HttpInterceptor", "contents": "\n\n
\n
\n
\n \n API > @angular/common > @angular/common/http\n
\n \n
\n \n
\n

HttpInterceptorlink

\n \n \n \n \n \n
\n \n \n\n
\n \n
\n

Intercepts and handles an HttpRequest or HttpResponse.

\n\n

See more...

\n
\n \n \n
\n\ninterface HttpInterceptor {\n intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>>\n}\n\n\n \n \n\n\n \n \n\n
\n\n \n
\n

See alsolink

\n \n
\n\n\n \n \n
\n

Descriptionlink

\n

Most interceptors transform the outgoing request before passing it to the\nnext interceptor in the chain, by calling next.handle(transformedReq).\nAn interceptor may transform the\nresponse event stream as well, by applying additional RxJS operators on the stream\nreturned by next.handle().

\n

More rarely, an interceptor may handle the request entirely,\nand compose a new event stream instead of invoking next.handle(). This is an\nacceptable behavior, but keep in mind that further interceptors will be skipped entirely.

\n

It is also rare but valid for an interceptor to return multiple responses on the\nevent stream for a single request.

\n\n

Further information available in the Usage Notes...

\n
\n\n \n\n \n\n
\n

Methodslink

\n \n \n\n \n \n \n \n \n \n \n \n \n \n\n \n\n \n \n
\n
\n

\n intercept()\n \n link

\n \n
\n
\n

Identifies and handles a given HTTP request.

\n\n
\n
\n \n\n intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>>\n\n \n\n
Parameters
\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
\n \n req\n HttpRequest\n

The outgoing request object to handle.

\n\n
\n \n next\n HttpHandler\n

The next interceptor in the chain, or the backend\nif no interceptors remain in the chain.

\n\n
\n\n \n
Returns
\n

Observable<HttpEvent<any>>: An observable of the event stream.

\n\n \n\n\n \n\n \n
\n
\n\n \n
\n\n\n \n
\n

Usage noteslink

\n

To use the same instance of HttpInterceptors for the entire app, import the HttpClientModule\nonly in your AppModule, and add the interceptors to the root application injector .\nIf you import HttpClientModule multiple times across different modules (for example, in lazy\nloading modules), each import creates a new copy of the HttpClientModule, which overwrites the\ninterceptors provided in the root module.

\n\n
\n\n\n\n
\n
\n\n\n" }