1

I show an alert based on the http response returned in Angular. My code works in Get and post methods, but not in put and delete methods.I'm stuck here and would be very happy if someone could help me. ApiUrl.ts => export const AlertUrls: string[] = [ // backend service urls ]

my-interceptor.ts => import { AlertUrls, collection } from "../ApiUrl";

  return next.handle(req).pipe(
    map((response: any) => {
        if ( AlertUrls.includes(req.url) && req.method !=='GET' && response instanceof HttpResponse && response.status === 200) {
          this.alertService.showSuccessAlert();
        }
    return response;
  }),
5
  • put a debugger on the if condition and check it manually! one of the if conditions is evaluating to false, should be done by you! Commented May 3, 2024 at 11:26
  • @NarenMurali Actually When the debugger is opened, while the post method is running, next to the following value => map((response: any) => { response = HttpResponse { header: HttpHeaders, status: 200, statusText: 'OK', url: '(backend service url)', ok: true, ...} A line like this appears and when I continue the debugger, the same line appears next to this code: response instanceof HttpResponse &&. Commented May 3, 2024 at 11:51
  • @NarenMurali However, when the debugger is turned on and the put and delete methods are running, next to the value here, map((response: any) => the line above is output. response instanceof HttpResponse && does not write any code next to the value here. Do you think the problem might be related to this? Commented May 3, 2024 at 11:51
  • 1
    you can just console.log(AlertUrls.includes(req.url), req.method !=='GET', response instanceof HttpResponse, response.status === 200) to check all the individual if conditions, if the debugger is giving you issues! Commented May 3, 2024 at 12:03
  • 1
    @NarenMurali Url is not mathcing completely with our delete url because of our delete item id. So it's not get in the if condition. That is the problem actually but i'm still figure out how can i solve this problem. Commented May 3, 2024 at 12:16

1 Answer 1

1

Url not matching is the problem you are facing, you can simply use the string property, includes to check if the string is present inside the url, instead of doing a full equality check!

AlertUrl: /delete

will match for

Full URL: /delete/123uuh4u23424

  return next.handle(req).pipe(
    map((response: any) => {
        if (AlertUrls.find((url: any) => req.url.includes(url)) && req.method !=='GET' && response instanceof HttpResponse && response.status === 200) {
          this.alertService.showSuccessAlert();
        }
    return response;
  }),
Sign up to request clarification or add additional context in comments.

1 Comment

I solved like this. return next.handle(req).pipe( map((response: any) => { console.log(AlertUrls) console.log(req.url) let cloneReq = req.url; let reqSplit = req.url.split('='); console.log(reqSplit) if(reqSplit.length>1){ cloneReq = reqSplit[0] + "=" ; } if ( AlertUrls.includes(cloneReq) && response instanceof HttpResponse && response.status === 200) { this.alertService.showSuccessAlert(); } return response; }),

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.