How can you log and inspect request and response headers in Angular

0 votes
can you tell me via code and example How can you log and inspect request and response headers in Angular?
Feb 25 in Angular by Nidhi
• 10,860 points
50 views

1 answer to this question.

0 votes

In Angular, you can log request and response headers using HttpInterceptor.

1. Create an Interceptor

Run:

ng generate service interceptors/logging

2. Implement Logging in Interceptor (logging.interceptor.ts)

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

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

import { Observable, tap } from 'rxjs';

@Injectable()

export class LoggingInterceptor implements HttpInterceptor {

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

    console.log('Request Headers:', req.headers);

    return next.handle(req).pipe(

      tap(event => {

        console.log('Response Headers:', event);

      })

    );

  }

}

3. Register the Interceptor in app.module.ts

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

import { LoggingInterceptor } from './interceptors/logging.interceptor';

@NgModule({

  providers: [

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

  ]

})

export class AppModule { }

answered Feb 25 by Navya

Related Questions In Angular

0 votes
1 answer

How to know tools and bundlers after create a new workspace or a project in angular?

Hello @sajal, When you create projects and workspaces ...READ MORE

answered Aug 6, 2020 in Angular by Niroj
• 82,840 points
1,138 views
0 votes
1 answer

How do I include a JavaScript script file in Angular and call a function from that script?

Hello @kartik, Refer the scripts inside the angular-cli.json (angular.json when using ...READ MORE

answered Sep 8, 2020 in Angular by Niroj
• 82,840 points
14,378 views
0 votes
1 answer

How do you handle uncaught exceptions and promise rejections in Express.js?

Handling uncaught exceptions and unhandled promise rejections ...READ MORE

answered Dec 6, 2024 in Angular by Navya
87 views
0 votes
1 answer

What are the vulnerability related to PHP Form?

Hii, The $_SERVER["PHP_SELF"] variable can be used by ...READ MORE

answered Feb 13, 2020 in PHP by Niroj
• 82,840 points
3,365 views
+1 vote
1 answer

How can we send message multiple time to a specific person or group in whatsapp using loop?

Hii @kartik,  This is simple task to send single ...READ MORE

answered Feb 28, 2020 in Java-Script by Niroj
• 82,840 points
19,346 views
0 votes
1 answer

Why it is necessary to refresh CSRF token per form request?

Hello, Generating a new CSRF token for each ...READ MORE

answered Mar 19, 2020 in Laravel by Niroj
• 82,840 points
4,792 views
0 votes
1 answer

What is meant by passing the variable by value and reference in PHP?

Hello, When the variable is passed as value ...READ MORE

answered Mar 27, 2020 in PHP by Niroj
• 82,840 points
3,395 views
0 votes
0 answers

Why would you use a service in Angular and how is it different from a component?

Can you explain with an example that ...READ MORE

6 days ago in Angular by Nidhi
• 10,860 points
26 views
0 votes
0 answers

How can we combine multiple built-in pipes in Angular templates?

Can you help me with a code ...READ MORE

6 days ago in Angular by Nidhi
• 10,860 points
27 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