devs don't know you can extract response headers with #Angular's HttpClient You just need to observe the entire response. not just the body: Provide example it

 To extract response headers with Angular's HttpClient, you can observe the full response using the observe: 'response' option. Here's an example:

typescript

import { HttpClient } from '@angular/common/http'; import { Injectable } from '@angular/core'; import { Observable } from 'rxjs'; @Injectable({ providedIn: 'root' }) export class ApiService { constructor(private http: HttpClient) {} getData(): Observable<any> { return this.http.get('https://example.com/api/data', { observe: 'response' }); } }

In the component, you can then access both the body and headers:

typescript

import { Component, OnInit } from '@angular/core'; import { ApiService } from './api.service'; @Component({ selector: 'app-my-component', templateUrl: './my-component.component.html', styleUrls: ['./my-component.component.css'] }) export class MyComponent implements OnInit { constructor(private apiService: ApiService) {} ngOnInit() { this.apiService.getData().subscribe(response => { console.log('Response Body:', response.body); console.log('Response Headers:', response.headers); }); } }

In this case, response.body will give you the data, while response.headers will give you the headers.

No comments:

Post a Comment

CPU vs GPU Architecture

  CPU vs GPU Architecture CPU (Central Processing Unit) and GPU (Graphics Processing Unit) have distinct architectural differences, optimize...

Best for you