gpt4 book ai didi

Angular HTTP拦截器如何链接一个可观察的

转载 作者:太空狗 更新时间:2023-10-29 18:12:43 25 4
gpt4 key购买 nike

我正在使用 Azure AD adal 库进行身份验证。有一个调用来获取返回可观察对象的 token 。如何将此可观察值添加到拦截中?在下面的示例中,我如何才能将订阅中设置的请求作为 Observable 返回?

  intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
this.authAzureService.getAccessToken()
.subscribe(token => {
// I need this to be returned
request = this.getRequestWithHeaders(request, token);
});

// This returns the request before the access token is added
return next.handle(request);
}

最佳答案

感谢@Commecial Suicide,我找到了解决方案,即使用平面 map 。这是有效的代码:

  intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
let requestHandler = this.authAzureService.getAccessToken()
.flatMap(token => {
request = this.getRequestWithHeaders(request, token);
return next.handle(request);
});
return requestHandler;
}

关于Angular HTTP拦截器如何链接一个可观察的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49437252/

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