Search code examples
angularangular2-services

Angular2 pass authorization inside header request


I am trying to authorize user by sending btoa encyption login string to request headers in angular2 but headers is sending authorization key inside request payload. Please refer to the screenshot

enter image description here

Login Service:

import { Http, Headers, Response, URLSearchParams } from '@angular/http';

let headers = new Headers();
headers.append('Content-Type', 'application/json');
headers.append('Authorization', loginString);

return this.http.post('http://localhost:9090/api/users/authenticate', 
{headers: headers}).map(res => res.json());    

I want to send Authorization key inside Request Headers instead of Request Payload

Thanks


Solution

  • You are passing your headers in the body parameter, try this

    import { Http, Headers, RequestOptions, Response, URLSearchParams } from '@angular/http';
    
    let headers = new Headers();
    headers.append('Content-Type', 'application/json');
    headers.append('Authorization', loginString);
    
    const options = new RequestOptions({headers: headers});
    
    return this.http.post('http://localhost:9090/api/users/authenticate', 
    null, options).map(res => res.json());