4

I'm making GET requests to an Express REST API, and returning data after setting res.status to 200. In Angular the response contains the data, but response.status is always undefined.

[EDIT START]

Following from Martin Adámek's answer below, I tried changing my get request to use an Observable rather than a Promise, and to include { observe: 'response' } in the request. But the response was exact the same - still no response.status.

Angular 4 get (with Observable)

requestNew(endPoint:string, type:string, query:string, data:any) {
  this.http[type](this.configService.apiHost + endPoint + query, data ? data : '', { observe: 'response' })
    .subscribe(
      (response:HttpResponse<any>) => {
        console.log(response) // Array containing expected data
        console.log(response.status) // undefined
      }
    )
}

[EDIT END]

Angular 4 get

import { HttpClient, HttpResponse, HttpErrorResponse } from '@angular/common/http';

request(endPoint:string, type:string, query:string, data:any) {
  let promise = new Promise((resolve, reject) => {
    this.http[type](this.configService.apiHost + endPoint + query, data ? data : '')
      .toPromise()
      .then((response: HttpResponse<any>) => {
        resolve(response);
      })
      .catch((error: HttpErrorResponse) => {
      })
  });
  return promise;
}

Express API:

router.get('/clients', (req, res) => {
  clientService.GetClients()
    .then((data) => {
      res.status(204).json(data); // data is sent
    })
})

How can I access response.status in Angular 4?

3
  • can you show us how you checking your status? Some logs? Commented Oct 31, 2017 at 12:26
  • I see you tried my answer. Next time ping me via comments so I know something went wrong :] I think the problem is that you are passing the {observe: 'response'} as third parameter, but for get, it should be second parameter, as there is no payload. Commented Nov 1, 2017 at 10:40
  • Btw this is not about observable/promise, feel free to stick to promises if you want. The important part is to add this option so you will get the whole response instead of just body. Commented Nov 1, 2017 at 10:41

1 Answer 1

2

HttpClient will automatically convert the payload from JSON and pass it to you. If you want to access the whole response object, you can do this:

http
  .get('/data.json', {observe: 'response'})
  .subscribe(resp => {
    console.log(resp.status);
  });

https://angular.io/guide/http#reading-the-full-response

https://angular.io/api/common/http/HttpResponse

Also note that GET and DELETE methods have different signature than POST and PUT, there is no payload parameter so you need to pass {observe: 'response'} as second parameter for those methods.

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.