Can i retry a call in response of an http call using interceptor without use of catchError
In angular 6 When iam calling a http service using interceptor if i got a response
{"status":403,"message":"Error in getting data try again"}
i want to recall the service again.
It is possible with catchError ,is there any other way to do it without catchError that is, i cant do it with error code . I want to do it with my status codes in response body
Following code when call success200 method, calling request again not working.its working only in catchError
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpSentEvent | HttpHeaderResponse | HttpProgressEvent | HttpResponse<any> | HttpUserEvent<any>> {
const authService = this.injector.get(UserTokenService);
return next.handle(this.addToken(req, authService.getToken())).pipe(
tap(
event =>
{
if (event instanceof HttpResponse) {
return this.success200(req, next,event.body);
}
},
error => status = 'failed'
),
catchError(error => {
//This will work
return next.handle(this.addToken(req, authService.getToken()));
})
);
}
//All Success request comes here
success200(req: HttpRequest<any>, next: HttpHandler,response){
const authService = this.injector.get(UserTokenService);
const routerService = this.injector.get(Router);
if(response.status=="403"){
//this call not working
return next.handle(this.addToken(req,authService.getToken()));
}
else{
return;
}
}
addToken(req: HttpRequest<any>, token: string): HttpRequest<any> {
//req = req.clone({ headers: req.headers.set('Accept', 'application/json') });
if(token != null) {
//set token header with all request
return req.clone({ setHeaders: { Authorization: token }});
}
else {
return req;
}
}
angular typescript angular6 angular-http-interceptors
add a comment |
In angular 6 When iam calling a http service using interceptor if i got a response
{"status":403,"message":"Error in getting data try again"}
i want to recall the service again.
It is possible with catchError ,is there any other way to do it without catchError that is, i cant do it with error code . I want to do it with my status codes in response body
Following code when call success200 method, calling request again not working.its working only in catchError
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpSentEvent | HttpHeaderResponse | HttpProgressEvent | HttpResponse<any> | HttpUserEvent<any>> {
const authService = this.injector.get(UserTokenService);
return next.handle(this.addToken(req, authService.getToken())).pipe(
tap(
event =>
{
if (event instanceof HttpResponse) {
return this.success200(req, next,event.body);
}
},
error => status = 'failed'
),
catchError(error => {
//This will work
return next.handle(this.addToken(req, authService.getToken()));
})
);
}
//All Success request comes here
success200(req: HttpRequest<any>, next: HttpHandler,response){
const authService = this.injector.get(UserTokenService);
const routerService = this.injector.get(Router);
if(response.status=="403"){
//this call not working
return next.handle(this.addToken(req,authService.getToken()));
}
else{
return;
}
}
addToken(req: HttpRequest<any>, token: string): HttpRequest<any> {
//req = req.clone({ headers: req.headers.set('Accept', 'application/json') });
if(token != null) {
//set token header with all request
return req.clone({ setHeaders: { Authorization: token }});
}
else {
return req;
}
}
angular typescript angular6 angular-http-interceptors
add a comment |
In angular 6 When iam calling a http service using interceptor if i got a response
{"status":403,"message":"Error in getting data try again"}
i want to recall the service again.
It is possible with catchError ,is there any other way to do it without catchError that is, i cant do it with error code . I want to do it with my status codes in response body
Following code when call success200 method, calling request again not working.its working only in catchError
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpSentEvent | HttpHeaderResponse | HttpProgressEvent | HttpResponse<any> | HttpUserEvent<any>> {
const authService = this.injector.get(UserTokenService);
return next.handle(this.addToken(req, authService.getToken())).pipe(
tap(
event =>
{
if (event instanceof HttpResponse) {
return this.success200(req, next,event.body);
}
},
error => status = 'failed'
),
catchError(error => {
//This will work
return next.handle(this.addToken(req, authService.getToken()));
})
);
}
//All Success request comes here
success200(req: HttpRequest<any>, next: HttpHandler,response){
const authService = this.injector.get(UserTokenService);
const routerService = this.injector.get(Router);
if(response.status=="403"){
//this call not working
return next.handle(this.addToken(req,authService.getToken()));
}
else{
return;
}
}
addToken(req: HttpRequest<any>, token: string): HttpRequest<any> {
//req = req.clone({ headers: req.headers.set('Accept', 'application/json') });
if(token != null) {
//set token header with all request
return req.clone({ setHeaders: { Authorization: token }});
}
else {
return req;
}
}
angular typescript angular6 angular-http-interceptors
In angular 6 When iam calling a http service using interceptor if i got a response
{"status":403,"message":"Error in getting data try again"}
i want to recall the service again.
It is possible with catchError ,is there any other way to do it without catchError that is, i cant do it with error code . I want to do it with my status codes in response body
Following code when call success200 method, calling request again not working.its working only in catchError
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpSentEvent | HttpHeaderResponse | HttpProgressEvent | HttpResponse<any> | HttpUserEvent<any>> {
const authService = this.injector.get(UserTokenService);
return next.handle(this.addToken(req, authService.getToken())).pipe(
tap(
event =>
{
if (event instanceof HttpResponse) {
return this.success200(req, next,event.body);
}
},
error => status = 'failed'
),
catchError(error => {
//This will work
return next.handle(this.addToken(req, authService.getToken()));
})
);
}
//All Success request comes here
success200(req: HttpRequest<any>, next: HttpHandler,response){
const authService = this.injector.get(UserTokenService);
const routerService = this.injector.get(Router);
if(response.status=="403"){
//this call not working
return next.handle(this.addToken(req,authService.getToken()));
}
else{
return;
}
}
addToken(req: HttpRequest<any>, token: string): HttpRequest<any> {
//req = req.clone({ headers: req.headers.set('Accept', 'application/json') });
if(token != null) {
//set token header with all request
return req.clone({ setHeaders: { Authorization: token }});
}
else {
return req;
}
}
angular typescript angular6 angular-http-interceptors
angular typescript angular6 angular-http-interceptors
asked 17 hours ago
Vishnudas
213
213
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Hoy about retry operator. For example:
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpSentEvent | HttpHeaderResponse | HttpProgressEvent | HttpResponse<any> | HttpUserEvent<any>> {
const authService = this.injector.get(UserTokenService);
return next.handle(this.addToken(req, authService.getToken())).pipe(
flatMap(
event =>
{
if (event instanceof HttpResponse) {
return this.success200(req, next,event.body);
}
else {
return throwError('Failed');
}
}
),
retry(1) // if you want to invoke 'next.handle' 1 additional time
);
}
I haven't tested this code but this is a general idea, you can see an example in the provided link below.
I have created this stackblitz example for you to see retry
in action.
UPDATE:
I did some additional research and found out that whenever 403 error occurs on http call, you will automatically enter the catch
block. I created another stackblitz example where you can see what happens on http requests for 200 and 403 responses. As for you example, you can also try something like this:
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpSentEvent | HttpHeaderResponse | HttpProgressEvent | HttpResponse<any> | HttpUserEvent<any>> {
const authService = this.injector.get(UserTokenService);
return next.handle(this.addToken(req, authService.getToken())).pipe(
flatMap(
event =>
{
if (event instanceof HttpResponse) {
return this.success200(req, next,event.body);
}
}
),
catchError(e => {
console.log('Failed attempt to login');
return throwError('Failed'); // retry logic should automatically kick in whenever _throwError_ occurs
}),
retry(1) // if you want 1 additional time
);
}
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%2f53942984%2fcan-i-retry-a-call-in-response-of-an-http-call-using-interceptor-without-use-of%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
Hoy about retry operator. For example:
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpSentEvent | HttpHeaderResponse | HttpProgressEvent | HttpResponse<any> | HttpUserEvent<any>> {
const authService = this.injector.get(UserTokenService);
return next.handle(this.addToken(req, authService.getToken())).pipe(
flatMap(
event =>
{
if (event instanceof HttpResponse) {
return this.success200(req, next,event.body);
}
else {
return throwError('Failed');
}
}
),
retry(1) // if you want to invoke 'next.handle' 1 additional time
);
}
I haven't tested this code but this is a general idea, you can see an example in the provided link below.
I have created this stackblitz example for you to see retry
in action.
UPDATE:
I did some additional research and found out that whenever 403 error occurs on http call, you will automatically enter the catch
block. I created another stackblitz example where you can see what happens on http requests for 200 and 403 responses. As for you example, you can also try something like this:
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpSentEvent | HttpHeaderResponse | HttpProgressEvent | HttpResponse<any> | HttpUserEvent<any>> {
const authService = this.injector.get(UserTokenService);
return next.handle(this.addToken(req, authService.getToken())).pipe(
flatMap(
event =>
{
if (event instanceof HttpResponse) {
return this.success200(req, next,event.body);
}
}
),
catchError(e => {
console.log('Failed attempt to login');
return throwError('Failed'); // retry logic should automatically kick in whenever _throwError_ occurs
}),
retry(1) // if you want 1 additional time
);
}
add a comment |
Hoy about retry operator. For example:
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpSentEvent | HttpHeaderResponse | HttpProgressEvent | HttpResponse<any> | HttpUserEvent<any>> {
const authService = this.injector.get(UserTokenService);
return next.handle(this.addToken(req, authService.getToken())).pipe(
flatMap(
event =>
{
if (event instanceof HttpResponse) {
return this.success200(req, next,event.body);
}
else {
return throwError('Failed');
}
}
),
retry(1) // if you want to invoke 'next.handle' 1 additional time
);
}
I haven't tested this code but this is a general idea, you can see an example in the provided link below.
I have created this stackblitz example for you to see retry
in action.
UPDATE:
I did some additional research and found out that whenever 403 error occurs on http call, you will automatically enter the catch
block. I created another stackblitz example where you can see what happens on http requests for 200 and 403 responses. As for you example, you can also try something like this:
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpSentEvent | HttpHeaderResponse | HttpProgressEvent | HttpResponse<any> | HttpUserEvent<any>> {
const authService = this.injector.get(UserTokenService);
return next.handle(this.addToken(req, authService.getToken())).pipe(
flatMap(
event =>
{
if (event instanceof HttpResponse) {
return this.success200(req, next,event.body);
}
}
),
catchError(e => {
console.log('Failed attempt to login');
return throwError('Failed'); // retry logic should automatically kick in whenever _throwError_ occurs
}),
retry(1) // if you want 1 additional time
);
}
add a comment |
Hoy about retry operator. For example:
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpSentEvent | HttpHeaderResponse | HttpProgressEvent | HttpResponse<any> | HttpUserEvent<any>> {
const authService = this.injector.get(UserTokenService);
return next.handle(this.addToken(req, authService.getToken())).pipe(
flatMap(
event =>
{
if (event instanceof HttpResponse) {
return this.success200(req, next,event.body);
}
else {
return throwError('Failed');
}
}
),
retry(1) // if you want to invoke 'next.handle' 1 additional time
);
}
I haven't tested this code but this is a general idea, you can see an example in the provided link below.
I have created this stackblitz example for you to see retry
in action.
UPDATE:
I did some additional research and found out that whenever 403 error occurs on http call, you will automatically enter the catch
block. I created another stackblitz example where you can see what happens on http requests for 200 and 403 responses. As for you example, you can also try something like this:
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpSentEvent | HttpHeaderResponse | HttpProgressEvent | HttpResponse<any> | HttpUserEvent<any>> {
const authService = this.injector.get(UserTokenService);
return next.handle(this.addToken(req, authService.getToken())).pipe(
flatMap(
event =>
{
if (event instanceof HttpResponse) {
return this.success200(req, next,event.body);
}
}
),
catchError(e => {
console.log('Failed attempt to login');
return throwError('Failed'); // retry logic should automatically kick in whenever _throwError_ occurs
}),
retry(1) // if you want 1 additional time
);
}
Hoy about retry operator. For example:
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpSentEvent | HttpHeaderResponse | HttpProgressEvent | HttpResponse<any> | HttpUserEvent<any>> {
const authService = this.injector.get(UserTokenService);
return next.handle(this.addToken(req, authService.getToken())).pipe(
flatMap(
event =>
{
if (event instanceof HttpResponse) {
return this.success200(req, next,event.body);
}
else {
return throwError('Failed');
}
}
),
retry(1) // if you want to invoke 'next.handle' 1 additional time
);
}
I haven't tested this code but this is a general idea, you can see an example in the provided link below.
I have created this stackblitz example for you to see retry
in action.
UPDATE:
I did some additional research and found out that whenever 403 error occurs on http call, you will automatically enter the catch
block. I created another stackblitz example where you can see what happens on http requests for 200 and 403 responses. As for you example, you can also try something like this:
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpSentEvent | HttpHeaderResponse | HttpProgressEvent | HttpResponse<any> | HttpUserEvent<any>> {
const authService = this.injector.get(UserTokenService);
return next.handle(this.addToken(req, authService.getToken())).pipe(
flatMap(
event =>
{
if (event instanceof HttpResponse) {
return this.success200(req, next,event.body);
}
}
),
catchError(e => {
console.log('Failed attempt to login');
return throwError('Failed'); // retry logic should automatically kick in whenever _throwError_ occurs
}),
retry(1) // if you want 1 additional time
);
}
edited 11 hours ago
answered 15 hours ago
miselking
1,63941927
1,63941927
add a comment |
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53942984%2fcan-i-retry-a-call-in-response-of-an-http-call-using-interceptor-without-use-of%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