0

I have following in angular 7.

public deleteId(pId){
    return this.http.delete<any>(this.deleteUrl(pId), {observe: 'response'})
      .pipe(catchError(this.handleError));
  }

Delete method (I want 202 code here in response.status)

public deleteMethod(): void {
    this.service.deleteId(this.id)
      .subscribe(response => {

       console.log(`Delete response ` , response);

        if(response.status === 200) {
          console.log('Deleted successfully');
        }
       else if(response.status === 202) {
          console.log('something else'); // I am not getting response.status here
        }
    });
  }

I am getting status code 200 properly but, 202 i am getting in handleError method. I want 202 as a response. How can i get that?

private handleError(error: HttpErrorResponse) {
  //It sends me to this function.
}
Paolo Casciello
  • 7,982
  • 1
  • 43
  • 42
ketan
  • 19,129
  • 42
  • 60
  • 98
  • Can you include what the `error` variable contains in the `handleResponse` method in the event of a 202 status? – Igor Sep 17 '19 at 14:47
  • @Igor What do you mean? I think this you are asking for`response.status` – ketan Sep 17 '19 at 14:53
  • @ketan it's really unclear what you are asking. Can you elaborate more? – Paolo Casciello Sep 17 '19 at 15:05
  • @PaoloCasciello I edited. When i run application API called and if response status is 202 it send me to `handleError` function instead of `.subscribe(response` – ketan Sep 17 '19 at 15:14

2 Answers2

0

Finally i got solution with following:

public deleteId(pId){
    return this.http.delete<any>(this.deleteUrl(pId), {observe: 'response', responseType: 'text' as 'json'})
      .pipe(catchError(this.handleError));
 }

Added , responseType: 'text' as 'json'

ketan
  • 19,129
  • 42
  • 60
  • 98
-1
public deleteMethod(): void {
this.service.deleteId(this.id)
  .subscribe(response => {

   console.log(`Delete response ` , response);

    if(response.status === 200) {
      console.log('Deleted successfully');
    }
  },
  err => {
    console.log(err.status); // ----> Here's your error
    if(err.status === 202) {
      // do your stuff
    }
  }
);

}

Goran Gajic
  • 185
  • 1
  • 13