how to pass basic auth for api in angular 11
import { HttpHeaders } from '@angular/common/http'; const httpOptions = { headers: new HttpHeaders({ 'Content-Type': 'application/json', 'Authorization': 'Basic ' + btoa('username:password') }) };
Here is what the above code is Doing:
1. We are importing the HttpHeaders class from the @angular/common/http package and adding it to our imports array because we need it to create a request header.
2. We are creating a constant httpOptions and assigning it an object with a headers key. The value of the headers key is an instance of the HttpHeaders class.
3. We are setting the Content-Type header value to application/json.
4. We are setting the Authorization header value to Basic base64(username:password).