1

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!

2
  • 2
    Like that stackoverflow.com/a/50929021/9590251 Commented Dec 20, 2022 at 11:20
  • But when I add {oberseve:'response'} it says: "the method just expected 1-2 arguments", and I can't take off the header Commented Dec 20, 2022 at 11:25

1 Answer 1

2

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];
        }
    })
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.