一个请求集HTTP头 [英] Set HTTP header for one request
问题描述
我在我的应用程序一个特殊的要求,即需要基本身份验证,所以我需要设置该请求的授权头。我读到设置HTTP请求头的,但是从我所知道的,它会设置头所有请求的该方法。我有这样的事情在我的code:
I have one particular request in my app that requires Basic authentication, so I need to set the Authorization header for that request. I read about setting HTTP request headers, but from what I can tell, it will set that header for all requests of that method. I have something like this in my code:
$http.defaults.headers.post.Authorization = "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==";
不过,我不希望我的POST请求发送此标头中的每一个。有什么办法送头只为一个请求我想要什么?或做我有我的请求后删除它?
But I don't want every one of my post requests sending this header. Is there any way to send the header just for the one request I want? Or do I have to remove it after my request?
推荐答案
有一个在你传递给 $ HTTP
每呼叫头的配置对象的头参数:
There's a headers parameter in the config object you pass to $http
for per-call headers:
$http({method: 'GET', url: 'www.google.com/someapi', headers: {
'Authorization': 'Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ=='}
});
或者使用快捷方式:
$http.get('www.google.com/someapi', {
headers: {'Authorization': 'Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ=='}
});
有效参数列表是在 $ HTTP 服务的文档。
这篇关于一个请求集HTTP头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!