Angular 6 Interceptor not working as expected
I have written an Http Interceptor
in Angular 6 .
The goal is to reload the home page when 302 status
is returned. (any back end API returning 302 status )
I am currently not able to understand why the Interceptor
is behaving the way it is.
Below is the code :
@Injectable()
export class InvalidResponseHandler implements HttpInterceptor{
constructor(){
}
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
return next.handle(request).pipe(
map((event: HttpEvent<any>) => {
if(event instanceof HttpResponse){
if(event.status === 302){
console.log('HttpResponse 302 status ');
window.location.reload();
return EMPTY;
}
}
})
)
}
}
Actual Behaviour :
1)I am first accessing the home page explicitly. (e.g: /home)
2)But the strange thing is , the initial call to fetch home age itself is not happening
3)It seems request is blocked reaching the server.
4)When interceptor is removed , this initial call is happening (/home)
I am not able to understand why is this behaviour ?
Why the request is getting blocked as I am NOT tampering with request it should be passed as is to back end rest end point .
It should be intercepted only when HttpResponse
is returned with 302 error
code .
Can anybody please help here ?
angular6 angular-http-interceptors
add a comment |
I have written an Http Interceptor
in Angular 6 .
The goal is to reload the home page when 302 status
is returned. (any back end API returning 302 status )
I am currently not able to understand why the Interceptor
is behaving the way it is.
Below is the code :
@Injectable()
export class InvalidResponseHandler implements HttpInterceptor{
constructor(){
}
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
return next.handle(request).pipe(
map((event: HttpEvent<any>) => {
if(event instanceof HttpResponse){
if(event.status === 302){
console.log('HttpResponse 302 status ');
window.location.reload();
return EMPTY;
}
}
})
)
}
}
Actual Behaviour :
1)I am first accessing the home page explicitly. (e.g: /home)
2)But the strange thing is , the initial call to fetch home age itself is not happening
3)It seems request is blocked reaching the server.
4)When interceptor is removed , this initial call is happening (/home)
I am not able to understand why is this behaviour ?
Why the request is getting blocked as I am NOT tampering with request it should be passed as is to back end rest end point .
It should be intercepted only when HttpResponse
is returned with 302 error
code .
Can anybody please help here ?
angular6 angular-http-interceptors
add a comment |
I have written an Http Interceptor
in Angular 6 .
The goal is to reload the home page when 302 status
is returned. (any back end API returning 302 status )
I am currently not able to understand why the Interceptor
is behaving the way it is.
Below is the code :
@Injectable()
export class InvalidResponseHandler implements HttpInterceptor{
constructor(){
}
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
return next.handle(request).pipe(
map((event: HttpEvent<any>) => {
if(event instanceof HttpResponse){
if(event.status === 302){
console.log('HttpResponse 302 status ');
window.location.reload();
return EMPTY;
}
}
})
)
}
}
Actual Behaviour :
1)I am first accessing the home page explicitly. (e.g: /home)
2)But the strange thing is , the initial call to fetch home age itself is not happening
3)It seems request is blocked reaching the server.
4)When interceptor is removed , this initial call is happening (/home)
I am not able to understand why is this behaviour ?
Why the request is getting blocked as I am NOT tampering with request it should be passed as is to back end rest end point .
It should be intercepted only when HttpResponse
is returned with 302 error
code .
Can anybody please help here ?
angular6 angular-http-interceptors
I have written an Http Interceptor
in Angular 6 .
The goal is to reload the home page when 302 status
is returned. (any back end API returning 302 status )
I am currently not able to understand why the Interceptor
is behaving the way it is.
Below is the code :
@Injectable()
export class InvalidResponseHandler implements HttpInterceptor{
constructor(){
}
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
return next.handle(request).pipe(
map((event: HttpEvent<any>) => {
if(event instanceof HttpResponse){
if(event.status === 302){
console.log('HttpResponse 302 status ');
window.location.reload();
return EMPTY;
}
}
})
)
}
}
Actual Behaviour :
1)I am first accessing the home page explicitly. (e.g: /home)
2)But the strange thing is , the initial call to fetch home age itself is not happening
3)It seems request is blocked reaching the server.
4)When interceptor is removed , this initial call is happening (/home)
I am not able to understand why is this behaviour ?
Why the request is getting blocked as I am NOT tampering with request it should be passed as is to back end rest end point .
It should be intercepted only when HttpResponse
is returned with 302 error
code .
Can anybody please help here ?
angular6 angular-http-interceptors
angular6 angular-http-interceptors
asked Jan 19 at 9:36
AtulAtul
6661440
6661440
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
The problem is that you forgot to return value in map operator. The main goal of map operator is to modify data and RETURN it back.
map((event: HttpEvent<any>) => {
if(event instanceof HttpResponse){
if(event.status === 302){
console.log('HttpResponse 302 status ');
window.location.reload();
return EMPTY;
}
}
return event; // here you return value back
})
Hi Dmitriy , It was right . Returning event from map operator resolved the issue
– Atul
Jan 21 at 11:31
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54265762%2fangular-6-interceptor-not-working-as-expected%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
The problem is that you forgot to return value in map operator. The main goal of map operator is to modify data and RETURN it back.
map((event: HttpEvent<any>) => {
if(event instanceof HttpResponse){
if(event.status === 302){
console.log('HttpResponse 302 status ');
window.location.reload();
return EMPTY;
}
}
return event; // here you return value back
})
Hi Dmitriy , It was right . Returning event from map operator resolved the issue
– Atul
Jan 21 at 11:31
add a comment |
The problem is that you forgot to return value in map operator. The main goal of map operator is to modify data and RETURN it back.
map((event: HttpEvent<any>) => {
if(event instanceof HttpResponse){
if(event.status === 302){
console.log('HttpResponse 302 status ');
window.location.reload();
return EMPTY;
}
}
return event; // here you return value back
})
Hi Dmitriy , It was right . Returning event from map operator resolved the issue
– Atul
Jan 21 at 11:31
add a comment |
The problem is that you forgot to return value in map operator. The main goal of map operator is to modify data and RETURN it back.
map((event: HttpEvent<any>) => {
if(event instanceof HttpResponse){
if(event.status === 302){
console.log('HttpResponse 302 status ');
window.location.reload();
return EMPTY;
}
}
return event; // here you return value back
})
The problem is that you forgot to return value in map operator. The main goal of map operator is to modify data and RETURN it back.
map((event: HttpEvent<any>) => {
if(event instanceof HttpResponse){
if(event.status === 302){
console.log('HttpResponse 302 status ');
window.location.reload();
return EMPTY;
}
}
return event; // here you return value back
})
answered Jan 19 at 19:12
Dmitriy KavraiskyiDmitriy Kavraiskyi
11810
11810
Hi Dmitriy , It was right . Returning event from map operator resolved the issue
– Atul
Jan 21 at 11:31
add a comment |
Hi Dmitriy , It was right . Returning event from map operator resolved the issue
– Atul
Jan 21 at 11:31
Hi Dmitriy , It was right . Returning event from map operator resolved the issue
– Atul
Jan 21 at 11:31
Hi Dmitriy , It was right . Returning event from map operator resolved the issue
– Atul
Jan 21 at 11:31
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54265762%2fangular-6-interceptor-not-working-as-expected%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown