我想将请求(正文)的值保存在变量中,以便稍后在代码中使用。
我正在用console.log做一些测试。
这有效:
const request = require("request");
var options = {
url: 'https://host/api/session',
method: 'POST',
headers: {
"Content-Type": "application/json"
},
rejectUnauthorized: false,
json: true,
body: {
username: "user",
password: "password"
}
};
request(options, function(error, response, body) {
console.log(body);
}
});
这不起作用。它给出了undefined
:
const request = require("request");
var options = {
url: 'https://host/api/session',
method: 'POST',
headers: {
"Content-Type": "application/json"
},
rejectUnauthorized: false,
json: true,
body: {
username: "user",
password: "password"
}
};
function loginSession() {
request(options, function(error, response, body) {
console.log(body);
}
});
};
loginSession();