How can I make a method that return the status Code of the request?
I'm using angular 15.
My get method in service:
public getData( ano: any, mes: any, glosadas: any, pagina:any): Observable<any> {
const token = this.token.retornaToken();
const headers = new HttpHeaders({ Authorization: `Bearer ${token}` });
return this.http.get(`API/?TpoRelatorio=1
&SomenteGlosadas=${glosadas}
&Ano=${ano}
&Mes=${mes}
&Page=${pagina}
&Count=0`,{ headers }
)
}
I want the status code for this method in component.ts
todasGuias() {
this.paginaBusca = 1;
while (this.paginaBusca <= this.listaGuias.TotalPages) {
this.homeService
.getData( this.ano, this.mes, this.glosadas, this.paginaBusca)
.subscribe((data) => {
if (data.status == 200) {
this.listaGuias = data.Dados[0];
this.listaTodasGuias.push(this.listaGuias.ResultList);
console.log(this.listaGuias);
console.log(this.listaTodasGuias);
var options = {
fieldSeparator: ',',
quoteStrings: '"',
decimalseparator: '.',
showLabels: true,
showTitle: true,
title: 'Consulta',
useBom: true,
headers: [ 'Guia', 'Tipo', 'Protocolo', 'Valor Informado', 'Valor Total', 'Cliente' ],
};
new ngxCsv(this.listaTodasGuias, 'Consulta', options);
}
});
this.paginaBusca ;
}
I don't know if it's rigth, just looking for some help!
CodePudding user response:
You need to add the observe: 'response'
option in the http request inside your getData method:
public getData( ano: any, mes: any, glosadas: any, pagina:any): Observable<any> {
const token = this.token.retornaToken();
const headers = new HttpHeaders({ Authorization: `Bearer ${token}` });
return this.http.get(`API/?TpoRelatorio=1
&SomenteGlosadas=${glosadas}
&Ano=${ano}
&Mes=${mes}
&Page=${pagina}
&Count=0`,
{ headers, observe: 'response' }
)
}
This way you'll get the complete response back.
In your components subscription you can access the HTTP status code with data.status
and the http response body with data.body
:
this.homeService
.getData( this.ano, this.mes, this.glosadas, this.paginaBusca)
.subscribe(data => {
if (data.status === 200) {
this.listaGuias = data.body.Dados[0];
}
})