我有这个拦截器:
import {Observable} from 'rxjs/Observable';
import {Injectable} from '@angular/core';
import {HttpEvent, HttpInterceptor, HttpHandler, HttpRequest, HttpErrorResponse, HttpParams} from '@angular/common/http';
import {SessionStorageService} from 'ng2-webstorage';
@Injectable()
export class HttpInterceptorService implements HttpInterceptor {
constructor(private sessionStorage: SessionStorageService) {
}
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
const url = '/';
req = req.clone({
url: url + req.url,
headers: req.headers.set('Authorization', this.sessionStorage.retrieve('access_token'))
});
return next.handle(req).catch(err => {
if (err instanceof HttpErrorResponse) {
console.log('interceptor error');
console.log(err);
if (err.status === 401) {
return Observable.throw(err);
}
}
});
}
}
我需要的是在当前过期时获得一个新令牌(使用params的某些'url'的结束请求)以及如果我在任何请求上得到响应401,那么保持请求(和所有其他请求)直到我得到一个新的令牌。如果 refresh_token 也已过期(也获得401),那么就做其他事情。
params: new HttpParams().set('Token', this.sessionStorage.retrieve('refresh_token'))
我无法理解如何申请新令牌。
我接下来做了什么
if (err.status === 401) {
const params = new HttpParams().set('token', this.sessionStorage.retrieve('refresh_token'));
req = req.clone({
method:'post',
url: url + '/auth/refresh',
params: params,
headers: req.headers.set('Authorization', this.sessionStorage.retrieve('refresh_token'))
});
next.handle(req).subscribe(res => {
console.log('res',res);
});
return Observable.throw(err);
}
但我的res
body
和我的数据不需要data
。我怎样才能从body
获得res.body
?
当我尝试执行res._body
或res.text()
或Property '_body' does not exist on type 'HttpEvent<any>'.
Property '_body' does not exist on type 'HttpSentEvent'.
之类的操作时,我收到如下错误:
res['body']
JSON.parse(res['body']);
帮助我。但我得到一个字符串数据。当我尝试cancel
时,我的请求状态转到#[derive(Debug)]
struct Node {
val: usize,
nodes: Vec<Node>,
}
fn find_max(node: &mut Node, val: usize) -> Option<&mut Node> {
if node.val < val {
return None;
}
let mut max_val = node.val;
let mut max: Option<&mut Node> = Some(node);
for n in &mut node.nodes {
if let Some(m) = find_max(n, max_val) {
max_val = m.val;
max = Some(m);
}
}
max
}
fn main() {
let mut root = Node {
val: 1,
nodes: vec![
Node {
val: 2,
nodes: vec![],
},
Node {
val: 3,
nodes: vec![
Node {
val: 4,
nodes: vec![],
},
],
},
],
};
println!("{:?}", find_max(&mut root, 0));
}
。我无法提出要求。
答案 0 :(得分:0)
来自next.handle(req)
的回复的类型为HttpEvent
,如果您对您必须执行的实际回复事件感兴趣:
if (res instanceof HttpResponse) {
const body = res.body;
// to something with the response body
}
更多信息可以在docs找到。