Search code examples
angulartypescriptangular-httpclient

Handle status in angular


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.
}

Solution

  • 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'