我正在使用 Nodejs 的 request
模块来发出 http 请求,但是 headers
对象存在一个问题:值不能包含双引号,否则会被区别对待。
基本上,我调用的 API 需要 headers
携带属性“X-Accesstoken”。
我的代码:
var userId = "123";
var url = "/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 Advanced REST API 客户端,我意识到问题是由于 headers
内的 X-Accesstoken
中的双引号 (") 造成的。
在这种情况下如何发送不带双引号的请求 header ?
更新:
header
拼写错误是根本原因,而不是双引号或大写“X-Accesstoken”。当我使用高级 REST 客户端发送请求时,它发送双引号作为 header 值的一部分,从而使我的 token 无效。
最佳答案
不应该是headers
而不是header
吗?
http://nodejs.org/api/http.html#http_http_request_options_callback
关于json - 如何使用nodejs请求模块正确发送请求头?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21971114/