{"id":323,"date":"2017-07-24T04:03:00","date_gmt":"2017-07-24T02:03:00","guid":{"rendered":"https:\/\/celilsemi.erkiner.com\/blog\/?p=323"},"modified":"2020-05-30T04:13:51","modified_gmt":"2020-05-30T02:13:51","slug":"how-to-create-an-angular-httpinterceptor","status":"publish","type":"post","link":"https:\/\/celilsemi.erkiner.com\/blog\/how-to-create-an-angular-httpinterceptor\/","title":{"rendered":"How to Create an Angular HttpInterceptor?"},"content":{"rendered":"\n<p>Angular 4.3\u00a0brought us\u00a0a new easier way to handle HTTP requests with the\u00a0HttpClient\u00a0library. I mentioned how powerful this tool is in my previous post; <a href=\"https:\/\/celilsemi.erkiner.com\/blog\/introduction-to-angular-httpclient-library\/\">Introduction to Angular HttpClient<\/a><\/p>\n\n\n\n<p>Now it\u2019s time to explore another of the powerful angular features:\u00a0interceptors. Interceptors provide a mechanism to intercept and\/or mutate outgoing requests or incoming responses.<\/p>\n\n\n\n<p>Interceptors are a way to do some work for every single HTTP request or response.<\/p>\n\n\n\n<p>Here are a few examples of common use cases for interceptors:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Add a token or some custom HTTP header for all outgoing HTTP requests<\/li><li>Catch HTTP responses to do some custom formatting (i.e. convert CSV to JSON) before handing the data over to your service\/component<\/li><li>Log all HTTP activity in the console<\/li><\/ul>\n\n\n\n<p>To implement an interceptor, you\u2019ll want to create a class that\u2019s injectable and that implements&nbsp;HttpInterceptor. The class should define an&nbsp;intercept&nbsp;method to correctly implement&nbsp;<strong>HttpInterceptor<\/strong>. The intercept method takes two arguments,&nbsp;req&nbsp;and&nbsp;next, and returns an observable of type&nbsp;HttpEvent.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>req&nbsp;is the request object itself and is of type&nbsp;HttpRequest.<\/li><li>next&nbsp;is the http handler, of type&nbsp;HttpHandler. The handler has a&nbsp;handle&nbsp;method that returns our desired&nbsp;<strong>HttpEvent<\/strong>&nbsp;observable.<\/li><\/ul>\n\n\n\n<p>Below is a basic interceptor implementation;<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:false,&quot;languageLabel&quot;:false,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;javascript&quot;,&quot;mime&quot;:&quot;application\/typescript&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;TypeScript&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;typescript&quot;}\">import { Injectable } from '@angular\/core';\nimport { HttpEvent, HttpRequest, HttpHandler, HttpInterceptor, HttpResponse } from '@angular\/common\/http';\nimport { Observable, of } from 'rxjs';\nimport { tap, delay } from 'rxjs\/operators';\n\n@Injectable()\nexport class AngularInterceptor implements HttpInterceptor {\n  intercept(request: HttpRequest&lt;any&gt;, next: HttpHandler): Observable&lt;HttpEvent&lt;any&gt;&gt; {\n    \n    \/\/ DO ANYTHING YOU WANT\n    \n    return next.handle(request);\n  }\n}<\/pre><\/div>\n\n\n\n<p>To wire-up our interceptor, provide it in the app module or a feature module using the\u00a0HTTP_INTERCEPTORS\u00a0token:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:false,&quot;languageLabel&quot;:false,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;javascript&quot;,&quot;mime&quot;:&quot;application\/typescript&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;TypeScript&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;typescript&quot;}\">@NgModule({\n  declarations: [\n    ...\n  ],\n  imports: [\n    ...\n  ],\n  providers: [\n  { provide: HTTP_INTERCEPTORS, useClass: AngularInterceptor, multi: true },\n    ...\n  ],\n  bootstrap: [AppComponent]\n})\nexport class AppModule {}\n<\/pre><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Angular 4.3\u00a0brought us\u00a0a new easier way to handle HTTP requests with the\u00a0HttpClient\u00a0library. I mentioned how powerful this tool is in my previous post; Introduction to Angular HttpClient Now it\u2019s time to explore another of the powerful angular features:\u00a0interceptors. Interceptors provide a mechanism to intercept and\/or mutate outgoing requests or incoming responses. Interceptors are a way [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"default","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"default","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[7],"tags":[22,4,51,46],"class_list":["post-323","post","type-post","status-publish","format-standard","hentry","category-front-end","tag-angular","tag-javascript","tag-rxjs","tag-typescript"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/celilsemi.erkiner.com\/blog\/wp-json\/wp\/v2\/posts\/323","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/celilsemi.erkiner.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/celilsemi.erkiner.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/celilsemi.erkiner.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/celilsemi.erkiner.com\/blog\/wp-json\/wp\/v2\/comments?post=323"}],"version-history":[{"count":2,"href":"https:\/\/celilsemi.erkiner.com\/blog\/wp-json\/wp\/v2\/posts\/323\/revisions"}],"predecessor-version":[{"id":325,"href":"https:\/\/celilsemi.erkiner.com\/blog\/wp-json\/wp\/v2\/posts\/323\/revisions\/325"}],"wp:attachment":[{"href":"https:\/\/celilsemi.erkiner.com\/blog\/wp-json\/wp\/v2\/media?parent=323"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/celilsemi.erkiner.com\/blog\/wp-json\/wp\/v2\/categories?post=323"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/celilsemi.erkiner.com\/blog\/wp-json\/wp\/v2\/tags?post=323"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}