0

I am hitting an api for getting userinfo, for that I am passing header authorization with value, but in options method it is firing and giving error that https status code 401, authentication required.

I am trying the same with Chrome browser and Postman client tool then it's giving me desired result. Why only my app is throwing such error? It's supposed to give me result if I am giving authorization header for that.

For instance, code is like:

this.http.get(this.url,{headers:this.headers});
Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
  • and how do you set your `authorization header` ? – KavehG Feb 09 '18 at 18:15
  • 1
    show code or don't expect help. You should read [how to ask questions](https://stackoverflow.com/help/mcve) – Mike Tung Feb 09 '18 at 18:24
  • its very simple const header = new header(); header.append('Authorization','Basic secretbase64string'); – Aditya Vashishtha Feb 09 '18 at 18:25
  • @kavehG there is nothing special in the code ,as I already provide u how I am calling , I just set header and passed it in http request . – Aditya Vashishtha Feb 09 '18 at 18:28
  • @AdityaVashishtha I found what is wrong, check out my answer. Actually there was no need to show the authorization header code ;) :D – KavehG Feb 09 '18 at 18:42
  • I believe the issue you're running into is a CORS "preflight" (preflight= OPTIONS) request. I was looking for where this is documented but I can't find it, but I don't believe that Chrome sends creds on a preflight request which is why you get the error in Chrome but not post man. Super good answer on CORS issues here https://stackoverflow.com/questions/43871637/no-access-control-allow-origin-header-is-present-on-the-requested-resource-whe/43881141#43881141 – Woot Feb 09 '18 at 18:43

2 Answers2

0

Try setting withCredentials : true .

Here is my Working Code. Hope it helps. Also note Iam using new httpclient of Angular 5.

emailsignin(username: string, password: string) { .
    let url = this.backendUrl + "/index";   
    let params = 'username='+username+'&password='+password;
    let httpheaders = new HttpHeaders(
        {
        'Content-Type': 'application/x-www-form-urlencoded'
        });

    return this._httpclient.post(url, params, {headers: httpheaders, withCredentials : true});

}     
Sivadinesh
  • 157
  • 2
  • 5
  • 15
0

You should define your options as a RequestOptions object:

let options = new RequestOptions({ headers: new Headers({ 'Authorization': 'Basic xxxx' }) });
this.http.get(this.url,options );
KavehG
  • 303
  • 1
  • 2
  • 11
  • @kavegh you can define that as requestOPtions butwhat i did is also valid it does not throw any error ,and also accept object to be passed like that , it is not an issue . – Aditya Vashishtha Feb 09 '18 at 19:35
  • @AdityaVashishthaa did you check yor request header with something like fiddler, comparing it with workin request that postman sends? – KavehG Feb 09 '18 at 19:37
  • @kavegh Yes i checked with postman with get as well as options method too, for get method it is giving me back results ,for options method it is also returing 200 status code without any error . – Aditya Vashishtha Feb 09 '18 at 19:47