Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to get statusCode in angular

How can I make a method that return the status Code of the request?

I’m using angular 15.

My get method in service:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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!

>Solution :

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];
        }
    })
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading