In an Angular service using HttpClient
, I've created a method that returns the raw HTTP response from a POST
:
httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json'
//observe: 'response'
}),
observe: 'response' as 'body'
};
forgotLogin(requestData: PUMRequestData): Observable<HttpResponse<string>> {
return this.http.post<HttpResponse<string>>("http://...", requestData, this.httpOptions);
Then in my component, I just look at the status and body from the raw http response.
this.api.forgotLogin(pumRequestData).subscribe(response => {
this.hStatus = response.status;
this.hBody = response.body;
});
The problem is that if the response status is not in the 200
series, then HttpClient
throws an error. In my use case, I just want to get the request status and body regardless of what the status is and I want to decide on what action to take based on the status in my component, not in an error handler in the service. I've been trying to figure some tricky way to create an error handler in the service that simply return the (Observable) HTTP
response, and haven't been able to - not even sure if that's the right approach.
So how can I get the raw http response back to my component when the status is not 200 series?