How to use interceptors to modify HTTP requests and responses in Angular

0 votes
With the help of code can you explain How to use interceptors to modify HTTP requests and responses in Angular?
18 hours ago in Node-js by Nidhi
• 12,380 points
14 views

1 answer to this question.

0 votes

In Angular, interceptors are used to modify HTTP requests and responses globally. They are implemented using the HttpInterceptor interface.

Steps to Use Interceptors:

Create an Interceptor:

import { Injectable } from '@angular/core';

import { HttpInterceptor, HttpRequest, HttpHandler, HttpEvent } from '@angular/common/http';

import { Observable } from 'rxjs';

@Injectable()

export class MyInterceptor implements HttpInterceptor {

  intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {

    const modifiedReq = req.clone({

      headers: req.headers.set('Authorization', 'Bearer token')

    });

    return next.handle(modifiedReq);

  }

}

Register Interceptor in AppModule:

import { HTTP_INTERCEPTORS } from '@angular/common/http';

import { MyInterceptor } from './my-interceptor';

@NgModule({

  providers: [

    { provide: HTTP_INTERCEPTORS, useClass: MyInterceptor, multi: true }

  ]

})

export class AppModule { }

answered 16 hours ago by Anvi

Related Questions In Node-js

0 votes
0 answers

How to handle HTTP GET requests in Angular using HttpClient?

Can you explain with the help of ...READ MORE

2 days ago in Node-js by Ashutosh
• 22,810 points
23 views
0 votes
1 answer

How to split and modify a string in NodeJS?

Hello @kartik, Use split and map function: var str = "123, 124, 234,252"; var ...READ MORE

answered Oct 16, 2020 in Node-js by Niroj
• 82,840 points
1,398 views
0 votes
0 answers

How to use the call effect in redux-saga for API requests?

With the help of python programming How ...READ MORE

3 days ago in Node-js by Ashutosh
• 22,810 points
34 views
0 votes
1 answer

How to use middleware for logging actions and state changes in Redux?

To use middleware for logging actions and ...READ MORE

answered 18 hours ago in Node-js by Anvi
30 views
0 votes
1 answer

How to unsubscribe from an observable to prevent memory leaks in Angular?

In Angular, unsubscribe from observables to prevent ...READ MORE

answered 16 hours ago in Node-js by Anvi
13 views
0 votes
1 answer
0 votes
1 answer

How to create a simple React Element displaying "Hello, World!"?

You can create a simple React element ...READ MORE

answered 16 hours ago in Node-js by Anvi
13 views
0 votes
1 answer

How to use the render function to display a React component?

To display a React component, use the ...READ MORE

answered 16 hours ago in Node-js by Anvi
12 views
0 votes
1 answer

How to host MEAN stack application with Angular and nodejs on windows IIS

It's fine that you're using Angular. Be ...READ MORE

answered May 27, 2022 in Node-js by Neha
• 9,020 points
1,494 views
0 votes
1 answer

How to schedule a google meet and get the meet link in NodeJs?

To create a Google Meet, you'll need ...READ MORE

answered May 27, 2022 in Node-js by Neha
• 9,020 points
4,114 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP