I want to have some information about the response when I request an API with http in Ionic 2 / Angular 2. Informations like : response time, response size ... ect.
I use this code :
let url : "[myUrl]";
this.http.get(url).map(res => res.json()).subscribe(res => {
console.log(res);
});
I want to get the response header.
Someone know how to do that ? :)
Angular
Http
request returns anObservable
which contains all the information the server has passed. So you can accessheaders
from the response asres.headers
. To obtain the size of the response body you can use,res.headers.get('Content-Length')
assuming this is present in the headers. So it depends on the information the response carries rather not what angular provides.
Response time information depends on what exactly you are looking for. For server response time in
node.js
you can use this package. Then in response ,response time
can be obtained withres.headers.get('X-Response-Time')
If you want the total response time (including the network delay ) you will have use
JavaScript
timer and find the time difference between request and response.So the information you are looking for mainly relies on the server response rather than angular. And beware of
CORS
in browser (Access-Control-Expose-Headers
in response header ) . You can understand more about headers here. Hope it helps.You can. In the map function you can get the all the information regarding HTTP call. You can do whatever you want with the result (I've added only a console.log)