Ionic 2:在http请求中获取响应头 [英] Ionic 2 : Get the header of a response in http request
问题描述
我希望获得一些有关响应的信息,当我在Ionic 2 / Angular 2中使用http请求API时。
信息如:响应时间,响应大小等等。
我使用以下代码:
let url:[myUrl];
this.http.get(url).map(res => res.json())。subscribe(res => {
console.log(res);
}) ;
我希望得到响应头。
<有人知道如何做到这一点? :)
this.http.get(url).map(res => ; {
console.log(res.headers); //打印http标题
return res.json();
})。subscribe(res => {
console .log(res);
});
Angular Http
请求返回一个 Observable
,其中包含服务器传递的所有信息。因此,您可以从 res.headers
的响应中访问标题
。要获得您可以使用的响应主体的大小,
res.headers.get('Content-Length')$ c $假设这存在于标题中,那么c>
。所以它取决于响应的信息而不是角度提供的信息。
响应时间信息取决于您正在寻找的是什么。对于 node.js
中的服务器响应时间,您可以使用这个包。然后作为回应,可以用 $ b
res.headers.get获得
响应时间
'X-Response-Time')
如果您想要总响应时间(包括网络延迟),您将使用 JavaScript
定时器并查找请求和响应之间的时间差。
所以您要查找的信息主要依赖于服务器响应而不是角度。并且在浏览器( Access-Control-Expose-Headers
)中注意 CORS
。您可以了解更多有关标头的信息此处。希望它有帮助。
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 ? :)
this.http.get(url).map(res => {
console.log(res.headers); // Print http header
return res.json();
}).subscribe(res => {
console.log(res);
});
Angular Http
request returns an Observable
which contains all the information the server has passed. So you can access headers
from the response as res.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 with
res.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.
这篇关于Ionic 2:在http请求中获取响应头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!