How do I access response headers using HttpClient in Angular 5?

前端 未结 1 1723
广开言路
广开言路 2020-12-18 01:53

I have written an authentication service in Angular 5 which does a POST request to my backend using the HttpClient class. The backend responds by sending a JWT bearer token.

相关标签:
1条回答
  • 2020-12-18 02:20

    To access the full response (not just the body of the response), you must pass the observe: 'response' parameter option in your http request. Now you can access the headers with res.headers

    return this.http.post('http://127.0.0.1:8080/api/v1/login', {
            'username': username,
            'password': password
        }, {
            headers: new HttpHeaders()
                .set('Content-Type', 'application/json'),
            observe: 'response'
        })
        .map(res => {
            let myHeader = res.headers.get('my-header');
        });
    

    Docs

    0 讨论(0)
提交回复
热议问题