简体   繁体   English

角度休息api调用返回https状态码401,需要身份验证

[英]angular rest api call returns https status code 401 ,authentication required

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. 我正在点击一个用于获取userinfo的api,因为我正在传递带有值的标头授权,但是在options方法中它正在触发并给出错误,即https状态码401(需要身份验证)。

I am trying the same with Chrome browser and Postman client tool then it's giving me desired result. 我正在尝试使用Chrome浏览器和Postman客户端工具进行同样的操作,这给了我想要的结果。 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});

Try setting withCredentials : true . 尝试设置withCredentials:true。

Here is my Working Code. 这是我的工作代码。 Hope it helps. 希望能帮助到你。 Also note Iam using new httpclient of Angular 5. 还要注意Iam使用Angular 5的新httpclient。

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});

}     

You should define your options as a RequestOptions object: 您应该将选项定义为RequestOptions对象:

let options = new RequestOptions({ headers: new Headers({ 'Authorization': 'Basic xxxx' }) });
this.http.get(this.url,options );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM