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,160
  • 1
  • 17
  • 38
CodeMan
  • 1,941
  • 6
  • 26
  • 44

1 Answers1

0

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
  • 33,618
  • 10
  • 97
  • 105