尝试在节点中发送 header
Trying to send header in node
我正在尝试访问 api,它要求我在 GET
请求的 header 中发送一些信息,如下图所示。
我的代码是这样设置的,但是我收到 resp has no method setter
错误。我读过各种帖子,也看过其他语言的例子,但我在节点中不太明白。
https.get(url, function(resp){
resp.setHeader("Content-Type", "json/application");
resp.setHeader("Authorization", Key);
resp.on('data', function(chunk){
sentStr += chunk;
});
resp.on('end', function(){
console.log(sentStr);
});
});
您正在尝试为响应设置 headers,而请求是需要设置的。 http
和 https
采用 URL 或一组选项来开始调用。这是一个例子
var https = require('https');
var options = {
hostname: "www.google.com",
port: 443,
path: '/',
method: 'GET',
headers: {
"Content-Type": "json/application"
"Authorization" : "KEY YOU NEED TO SUPPORT"
}
}
https.get(options, function(res) {
console.log("statusCode: ", res.statusCode);
console.log("headers: ", res.headers);
res.on('data', function(d) {
process.stdout.write(d);
});
}).on('error', function(e) {
console.error(e);
});
我正在尝试访问 api,它要求我在 GET
请求的 header 中发送一些信息,如下图所示。
我的代码是这样设置的,但是我收到 resp has no method setter
错误。我读过各种帖子,也看过其他语言的例子,但我在节点中不太明白。
https.get(url, function(resp){
resp.setHeader("Content-Type", "json/application");
resp.setHeader("Authorization", Key);
resp.on('data', function(chunk){
sentStr += chunk;
});
resp.on('end', function(){
console.log(sentStr);
});
});
您正在尝试为响应设置 headers,而请求是需要设置的。 http
和 https
采用 URL 或一组选项来开始调用。这是一个例子
var https = require('https');
var options = {
hostname: "www.google.com",
port: 443,
path: '/',
method: 'GET',
headers: {
"Content-Type": "json/application"
"Authorization" : "KEY YOU NEED TO SUPPORT"
}
}
https.get(options, function(res) {
console.log("statusCode: ", res.statusCode);
console.log("headers: ", res.headers);
res.on('data', function(d) {
process.stdout.write(d);
});
}).on('error', function(e) {
console.error(e);
});