I am trying to access an API which requires Basic Authorization.
Using Postman or Advanced Rest API Tool, I am able to get the response. However when I'm trying to access through Angular 4 HttpClient I am getting this exception:
login:1 XMLHttpRequest cannot load https://someUrl.com:4400/api/v1/login. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:4200' is therefore not allowed access. The response had HTTP status code 405.
Here is the angular code
@Injectable()
export class LoginService {
constructor(private http: HttpClient, @Inject('api') private api) {
}
login(post) {
let auth="Basic "+ btoa(post.username + ":" + post.password);
console.log(auth);
let headers = new HttpHeaders({'Content-Type': 'application/json'})
.set("authorization", auth);
console.log(headers);
return this.http.post(this.api + 'login',null,{
headers:headers
});
}
}