Angular 2 multiple http requests based on status

49 Views Asked by At

I have question related to angular http requests. I'll send POST request with some data, if server return 404 status I need to modify data (in response body will be invalid data - I will remove them). I will repeat this action until I get response status 200. And now question :) How can I achieve it with angular 2 syntax?

1

There are 1 best solutions below

0
On

That will actually be a recursive call that you will make with modified data each time you get response code other than 200. Please see the following code snippet:

  myFunction(someData): void {

    this.myService.serviceFunction(someData).subscribe(
      response => {

        if (response.status !== 200) {
          //modify your data
          myFunction(modifiedData);
        }
      },
      err => {
          //deal error response
          if (err.status !== 200) {
          //modify your data
          myFunction(modifiedData); 
          }
      }
    );
  }