我正在从节点调用http post请求(来自其他第三方服务器的登录请求),并希望保存cookie以用于将来的http请求。
var options = {
host: 'hostname.com',
port: '80',
path: '/login.php',
method: 'POST'
};
var data = qs.stringify({
uname: "validUsername",
password: "validPassword"
});
var req = http.request(options, function(response) {
console.log(response.statusCode);
console.log('HEADERS: ' + JSON.stringify(response.headers));
// console.log('Cookies: ' + response.getHeader("Set-Cookie"));
response.setEncoding('utf8');
response.on('data', function (chunk) {
console.log('BODY: ' + chunk);
});
});
req.on('error', function(e) {
console.log('problem with request: ' + e.message);
});
req.write(data);
req.end();
如何从请求的服务器发送的响应中访问和保存cookie?