我正在使用request
模块为Nodejs发出http请求,但headers
对象存在问题:该值不能包含双引号,否则将采用不同的处理方式。如何使用nodejs请求模块正确发送请求标头?
基本上我打电话要求headers
携带一个属性“X-Accesstoken”的API。
我的代码:
var userId = "123";
var url = "https://stackoverflow.com/users/{id}".replace("{id}", userId) ;
var token = "abcd1234"; //changed to protect the innocence, anyway it'll be the valid generated token
var options = {
method: 'GET',
url: url,
header: {
"x-Accesstoken": token
"Content-Type": "application/json"
}
};
console.log('testing ' + url);
request(options, function(error,response,body){
console.log('body:' + body);
});
我总是得到这个错误:
body:'{
"status": 403,
"code": 0,
"reason": "Not authenticated"
}
然后,如果我使用Chrome高级REST API客户端,我意识到这个问题是因为双引号(” )在X-Accesstoken
内headers
我如何在这种情况下发送请求头没有双引号?
更新: header
错字是根本原因,而不是双引号或大写“X-Accesstoken”。当我使用高级REST客户端发送请求时,它将双引号作为标题值的一部分发送,因此使我的令牌无效。
1.您如何知道请求模块发送了带双引号的值? 2.你确定这不仅仅是因为你用小写字母“x”拼写了'x-Accesstoken'吗? –
代码中的令牌与您的屏幕截图不匹配。您确定'token' var在发送之前不包含嵌入的引号吗? – Joe
@AndreasHultgren HTTP头名不区分大小写,按照http://www.ietf.org/rfc/rfc2616.txt – Joe