How To Set Authorization Headers in GET/POST Requests?

What Is HttpInterceptor in Angular?

The Http Interceptor is an interface and used to implement the intercept method.

Example -
//Register an Interceptor (HTTP_INTERCEPTORS) in the app Module.
import {NgModule} from '@angular/core';
import {HTTP_INTERCEPTORS} from '@angular/common/http';

  providers: [{
    useClass: MyInterceptor,
    multi: true,
export class AppModule {}

//The HttpInterceptor is an interface and used to implement the intercept method.
import {Injectable} from '@angular/core';
import {HttpEvent, HttpInterceptor, HttpHandler, HttpRequest} from '@angular/common/http';

export class MyInterceptor implements HttpInterceptor {
  intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    const reqHeader = req.clone({headers: req.headers.set('Authorization', 'MyAuthToken')});
    return next.handle(reqHeader);

I hope you are enjoying with this post! Please share with you friends!! Thank you!!!

Hey! I'm Anil Singh. I author this blog. I'm Active Blogger, Programmer. I love learning new technologies, programming, blogging and participating the forum discussions more...
My Blogs - and
My Books - Interview Questions and Answers Books- Get Your Book in 15+ Digital Stores Worldwide..

You Might Also Like
Post a Comment Powered by Blogger.
ASK Questions