How does Angular s HttpInterceptor modify outgoing requests

0 votes
Can you tell me How does Angular's HttpInterceptor modify outgoing requests?
Feb 24 in Angular by Nidhi
• 10,400 points
32 views

1 answer to this question.

0 votes

An HttpInterceptor in Angular is used to modify outgoing HTTP requests before they are sent to the server. It is commonly used for adding authentication tokens, logging, or modifying headers.

Implementation Example: Adding an Authorization Header

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

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

@Injectable()

export class AuthInterceptor implements HttpInterceptor {

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

    const modifiedReq = req.clone({

      setHeaders: { Authorization: `Bearer your-token-here` }

    });

    return next.handle(modifiedReq);

  }

}

answered Feb 24 by Navya

Related Questions In Angular

0 votes
1 answer
0 votes
0 answers

How does the @Component directive define Angular components?

With the help of code and example ...READ MORE

5 days ago in Angular by Nidhi
• 10,400 points
20 views
0 votes
1 answer

How compile, controller, pre-linking and post linking works in Angularjs?

Explanation of compile and link process don't ...READ MORE

answered Jan 31, 2020 in Angular by Niroj
• 82,840 points
1,698 views
0 votes
1 answer

How can we achieve transclusion in AngularJs?

Hii,  In order to know what transclusion is ...READ MORE

answered Feb 5, 2020 in Angular by Niroj
• 82,840 points
760 views
0 votes
1 answer

Should I use map or switchmap when using angular http module?

When working with the Angular HTTP module, ...READ MORE

answered Feb 24 in Angular by Navya
32 views
0 votes
1 answer

How does BehaviorSubject differ from Subject in state management?

Feature Subject BehaviorSubject Initial Value No initial value Requires an initial value Last ...READ MORE

answered Feb 24 in Node-js by Navya
31 views
0 votes
1 answer
0 votes
1 answer

What is the use of takeUntil to cancel a subscription?

takeUntil is an RxJS operator used to ...READ MORE

answered Feb 24 in Node-js by Navya
40 views
0 votes
1 answer

How does Angular work?

Angular is a TypeScript-based front-end framework for ...READ MORE

answered 6 days ago in Angular by Navya
27 views
0 votes
1 answer

How to transfer data between two unrelated components in Angular?

Steps to Transfer Data Between Unrelated Components 1. ...READ MORE

answered Dec 12, 2024 in Angular by Navya
159 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