admin管理员组文章数量:1024074
I know that http 302
responses are handled directly by the browser, and because of that you cannot acces any of the request properties from your source code. But I am wondering if there is any way of intercepting the 302 redirect response. Let me explain myself:
- My Frontend (Angular) makes an http request to A (I intercept the outgoing request)
- A responds with
302 Location: B
- My Frontend intercepts the
302
response with empty fields, and goes to B - Here I'd like to intercept the response ing from B
This is my Angular http interceptor code:
@Injectable()
export class CasInterceptor implements HttpInterceptor {
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
console.log('->Interceptor');
console.log(req);
return next.handle(req).map((event: HttpEvent<any>) => {
const response = event as HttpResponseBase;
console.log('<-Interceptor');
console.log(response);
return event;
});
}
}
I know that http 302
responses are handled directly by the browser, and because of that you cannot acces any of the request properties from your source code. But I am wondering if there is any way of intercepting the 302 redirect response. Let me explain myself:
- My Frontend (Angular) makes an http request to A (I intercept the outgoing request)
- A responds with
302 Location: B
- My Frontend intercepts the
302
response with empty fields, and goes to B - Here I'd like to intercept the response ing from B
This is my Angular http interceptor code:
@Injectable()
export class CasInterceptor implements HttpInterceptor {
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
console.log('->Interceptor');
console.log(req);
return next.handle(req).map((event: HttpEvent<any>) => {
const response = event as HttpResponseBase;
console.log('<-Interceptor');
console.log(response);
return event;
});
}
}
Share
Improve this question
asked Feb 5, 2018 at 9:01
gmcgmc
4,0103 gold badges33 silver badges46 bronze badges
4
-
Browsers do support this but you'll have to check for it and there's no way to polyfill. You'll have to dig deeper in
HttpInterceptor
or write something from scratch. developer.mozilla/en-US/docs/Web/API/Request/redirect – Tatsh Commented Feb 5, 2018 at 9:09 - Isn't the eventual response from B just what you receive as the eventual response to the original http request? – match Commented Feb 5, 2018 at 9:09
- Not necessarily, that's why I'd like to intercept it: if the user is authenticated the result is the expected from the original request, if not it is a login form. I'd like to be able to check whether the login form came (well, just check if the content type is html) and manually redirect the browser without having to add that logic everywhere, just in the interceptor – gmc Commented Feb 5, 2018 at 9:13
- any solution yet? – Kwexi Commented Feb 23, 2018 at 4:19
1 Answer
Reset to default 1You should get full header from http response.
{observe:"response"} is the magic parameter of angular http client. So try this one
this.http
.get<any>(requestURL,{observe:"response"})
.subscribe(
data => {
console.log(data.header); //you will see full header here
console.log(data.url); // you can see redirect url from backend and handle it whatever you want
},
err => {
console.log(err)
}
I know that http 302
responses are handled directly by the browser, and because of that you cannot acces any of the request properties from your source code. But I am wondering if there is any way of intercepting the 302 redirect response. Let me explain myself:
- My Frontend (Angular) makes an http request to A (I intercept the outgoing request)
- A responds with
302 Location: B
- My Frontend intercepts the
302
response with empty fields, and goes to B - Here I'd like to intercept the response ing from B
This is my Angular http interceptor code:
@Injectable()
export class CasInterceptor implements HttpInterceptor {
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
console.log('->Interceptor');
console.log(req);
return next.handle(req).map((event: HttpEvent<any>) => {
const response = event as HttpResponseBase;
console.log('<-Interceptor');
console.log(response);
return event;
});
}
}
I know that http 302
responses are handled directly by the browser, and because of that you cannot acces any of the request properties from your source code. But I am wondering if there is any way of intercepting the 302 redirect response. Let me explain myself:
- My Frontend (Angular) makes an http request to A (I intercept the outgoing request)
- A responds with
302 Location: B
- My Frontend intercepts the
302
response with empty fields, and goes to B - Here I'd like to intercept the response ing from B
This is my Angular http interceptor code:
@Injectable()
export class CasInterceptor implements HttpInterceptor {
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
console.log('->Interceptor');
console.log(req);
return next.handle(req).map((event: HttpEvent<any>) => {
const response = event as HttpResponseBase;
console.log('<-Interceptor');
console.log(response);
return event;
});
}
}
Share
Improve this question
asked Feb 5, 2018 at 9:01
gmcgmc
4,0103 gold badges33 silver badges46 bronze badges
4
-
Browsers do support this but you'll have to check for it and there's no way to polyfill. You'll have to dig deeper in
HttpInterceptor
or write something from scratch. developer.mozilla/en-US/docs/Web/API/Request/redirect – Tatsh Commented Feb 5, 2018 at 9:09 - Isn't the eventual response from B just what you receive as the eventual response to the original http request? – match Commented Feb 5, 2018 at 9:09
- Not necessarily, that's why I'd like to intercept it: if the user is authenticated the result is the expected from the original request, if not it is a login form. I'd like to be able to check whether the login form came (well, just check if the content type is html) and manually redirect the browser without having to add that logic everywhere, just in the interceptor – gmc Commented Feb 5, 2018 at 9:13
- any solution yet? – Kwexi Commented Feb 23, 2018 at 4:19
1 Answer
Reset to default 1You should get full header from http response.
{observe:"response"} is the magic parameter of angular http client. So try this one
this.http
.get<any>(requestURL,{observe:"response"})
.subscribe(
data => {
console.log(data.header); //you will see full header here
console.log(data.url); // you can see redirect url from backend and handle it whatever you want
},
err => {
console.log(err)
}
本文标签: javascriptHandling http 302 redirect responsesStack Overflow
版权声明:本文标题:javascript - Handling http 302 redirect responses - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745607229a2158795.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论