I am trying to send a request to my backend which uses HTTP Basic auth for authentication. For testing purposes
username: user
password: password
so the correct header is:
Authorization: Basic dXNlcjpwYXNzd29yZA==
I have tested the request with this header in Chrome Advanced Rest Extension and it works:
I generated the request in Angular2 like this:
public getCurrentCounter() {
console.log("Method getCurrentCounter() in CounterService called");
var request = this.backendURL + "counter";
var header = this.generateHeader(this.username, this.password);
console.log(header);
return this._http.get(request, {
headers: header
})
.map(res => res.json());
}
/**
* Generate HTTP header using HTTP basic Auth
*/
private generateHeader(username, password) {
var base64Creds = btoa(username + ":" + password);
var auth = 'Basic ' + base64Creds;
console.log(auth);
var authHeader = new Headers();
authHeader.append("Authorization", auth);
return authHeader;
}
I logged the generated Header Object and it looks like this:
Still I get this response:
XMLHttpRequest cannot load http://localhost:8080/counter. Response for preflight has invalid HTTP status code 401
Anybody an idea what could be wrong?