1

How can I add a header for username and password for GET Request in http in angular2?

I am try to add httpclient and http import is not working. Can you suggest to me how to add a header in http and pass my username and password?

Mike Feltman
  • 5,131
  • 1
  • 15
  • 37
CodeMan
  • 1,841
  • 5
  • 24
  • 44

1 Answers1

1

You can set the header like this:

yourMethodWhereYouPost(userName:string, password:string) 
{
    // ....
    // Your code 
    //

    // Get the authorization header.
    const authHeader = this.getAuthorizationHeader(userName, password);
    const headers = new Headers({ 'Content-Type': 'application/json' });
    headers.append('Authorization', authHeader);
    // ....

    // Some more code
} 

private getAuthorizationHeader(userName: string, password: string): string 
{
    // Return the encoded auth header.
    return btoa(userName + ':' + password);
}
Faisal
  • 31,390
  • 9
  • 89
  • 101