gpt4 book ai didi

angular - 如何从 HttpClient 响应访问 header ? ( Angular/ ionic )

转载 作者:太空狗 更新时间:2023-10-29 18:08:01 49 4
gpt4 key购买 nike

我正在使用一个返回不记名 token 作为响应 header 的登录端点,正如我在“网络”Chrome 检查窗口中看到的那样:

Response Headers
Access-Control-Allow-Credentials:true
Access-Control-Allow-Origin:http://localhost:8100
Authorization:Bearer eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJuZWxpby5jdXJzb3NAZ21haWwuY29tIiwiZXhwIjoxNTEyNzA3OTQ3fQ.pOR4WrqkaFXdwbeod1tNlDniFZXTeMXzKz9uU68rLXEWDAVRgWIphvx5F_VCsXDwimD8Q04JrxelkNgZMzBgXA
Cache-Control:no-cache, no-store, max-age=0, must-revalidate
Content-Length:188
(etc...)

但是,当我尝试使用 HttpClient 实例从响应中打印“ header ”时:

  authenticate(credentials) {
let creds = JSON.stringify(credentials);
let contentHeader = new HttpHeaders({"Content-Type": "application/json"});
this.http.post(this.LOGIN_URL, creds, { headers: contentHeader, observe: 'response'})
.subscribe(
(resp) => {
console.log("resp-ok");
console.log(resp.headers);
},
(resp) => {
console.log("resp-error");
console.log(resp);
}
);
}

我得到一个完全不同的结构:

HttpHeaders {normalizedNames: Map(0), lazyUpdate: null, lazyInit: ƒ}
lazyInit : ƒ ()
lazyUpdate : null
normalizedNames : Map(0) {}

我也尝试了 get(headerName) 方法并得到了 null。我错过了什么?如何从我的响应中获取“授权” header ?

最佳答案

像这样尝试:

authenticate(credentials) {
let creds = JSON.stringify(credentials);
let contentHeader = new HttpHeaders({ "Content-Type": "application/json" });
this.http.post(this.LOGIN_URL, creds, { headers: contentHeader, observe: 'response' })
.subscribe(
(resp) => {
let header: HttpHeaders = resp.headers;
console.log(header.get('Authorization'))
},
(resp) => {
console.log("resp-error");
console.log(resp);
}
);
}

关于angular - 如何从 HttpClient 响应访问 header ? ( Angular/ ionic ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47687774/

49 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com