How to read Http response header in angular

Discussão em 'Angular' iniciado por jhone, Dezembro 28, 2024.

  1. jhone

    jhone Guest

    This is my angular service class code.I have called restful web service in this class ,response is coming . Response header has JWT web token I want to read response header and take JWT web token in this authUser service function. how to do it?

    import { Injectable } from '@angular/core';
    import {ResponseBeanModule} from '../../module/responsebean/responsebean.module';
    import { Http, Headers, Response } from '@angular/http';


    @Injectable({
    providedIn: 'root'
    })
    export class AuthenticationService {
    constructor(private http: Http) { this._prepare()}
    private userUrl = 'http://192.168.1.152:8080/api/user/';

    endpointUrl: String;
    responseBeanModule: ResponseBeanModule = new ResponseBeanModule();
    options: any;
    headers: any;

    _prepare () {
    this.endpointUrl = this.userUrl;
    this.headers = new Headers();
    this.headers.set('Content-Type', 'application/json');
    this.options = {
    headers: this.headers,
    observe: 'body'
    };
    }



    public authUser(loginBean): Promise<ResponseBeanModule> {
    return new Promise((resolve, reject) => {
    this.http.post(this.userUrl, loginBean, this.options).subscribe(
    (data) => {
    alert(JSON.stringify(data));
    this.responseBeanModule = JSON.parse(data['_body'])
    return resolve( this.responseBeanModule );
    },
    (err) => {
    return reject(err);
    }
    );
    });

    }

    }

    Continue reading...

Compartilhe esta Página