I have following service in Angular 7:
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { map } from 'rxjs/operators';
import { BehaviorSubject } from 'rxjs';
@Injectable()
export class UserService {
private currentUserSubject: BehaviorSubject<any>;
constructor(
private http: HttpClient
) {
this.currentUserSubject = new BehaviorSubject(null);
}
public get currentUserValue(): any {
return this.currentUserSubject.value;
}
login(entity: any, inputUsername: any, inputPassword: any): any {
const httpOptions = {
headers: new HttpHeaders({
username: inputUsername,
password: inputPassword
})
};
return this.http.get(entity.url, httpOptions)
.pipe(map((user: Response) => {
console.log(user);
console.log(user.status);
if (user) {
this.currentUserSubject.next(user);
}
return user;
}));
}
}
I want to get the response status (200, 401, etc.). If I try to subscribe such as .pipe(map(.....)).subscribe(...), I get error that subscribe is not a function.
Also, I get only the 200 status responses in pipe(map()). I do not get responses with other status codes here.
I want to update the BehaviorSubject based on the received status.
How can I get all the responses and their status in the service?
I have gone though Angular 6 Get response headers with httpclient issue, but that does not apply here.
I added observe: 'response' to http headers, but it did not make any difference.
const httpOptions = {
headers: new HttpHeaders({
username: inputUsername,
password: inputPassword,
observe: 'response',
}),
};
{observe: 'response'}
and it didnt work ? – Tony Ngo