node.js - access a returned header from previous post in nodejs
问题描述
I am making a POST in node.js. From there, I need to access an authentication-token header to make another POST with that authentication-token as a header value.
I am having issues getting the authentication-token out of the returned object.
var request = require('request-promise');
var uri = 'https://myurl/rest/login';
var _include_headers = function(body, response, resolveWithFullResponse) {
return {'headers': response.headers, 'data': body};
};
var options = {
method: 'POST',
uri: uri,
json: true,
transform: _include_headers,
body: { "username":"user", "password":"password"
},
}
request(options)
.then(function (parsedBody) {
console.log("post success");
})
.catch(function (err) {
console.log("post failed");
});
return request(options)
.then(function(response) {
// console.log(response.headers);
console.log(response.headers);
//var result = JSON.stringify(response.headers);
// var result = JSON.stringify(response.headers);
console.log(response.headers);
// console.log(response.data);
});
The output I am getting is below. I've tried turning it into a string, and accessing response.headers[0].authentication-token, but that returns with errors of undefined for the key pairs as followed.
Unhandled rejection TypeError: Cannot read property 'date' of undefined
at C:\Users\n0200675\testing\logintest.js:32:35
at tryCatcher (C:\Users\n0200675\testing\node_modules\bluebird\js\release\ut
il.js:16:23)
at Promise._settlePromiseFromHandler (C:\Users\n0200675\testing\node_modules
\bluebird\js\release\promise.js:512:31)
at Promise._settlePromise (C:\Users\n0200675\testing\node_modules\bluebird\j
s\release\promise.js:569:18)
at Promise._settlePromise0 (C:\Users\n0200675\testing\node_modules\bluebird\
js\release\promise.js:614:10)
at Promise._settlePromises (C:\Users\n0200675\testing\node_modules\bluebird\
js\release\promise.js:694:18)
at _drainQueueStep (C:\Users\n0200675\testing\node_modules\bluebird\js\relea
se\async.js:138:12)
at _drainQueue (C:\Users\n0200675\testing\node_modules\bluebird\js\release\a
sync.js:131:9)
at Async._drainQueues (C:\Users\n0200675\testing\node_modules\bluebird\js\re
lease\async.js:147:5)
at Immediate.Async.drainQueues (C:\Users\n0200675\testing\node_modules\blueb
ird\js\release\async.js:17:14)
at runCallback (timers.js:789:20)
at tryOnImmediate (timers.js:751:5)
at processImmediate [as _immediateCallback] (timers.js:722:5)
解决方案
我最终使用了这个。
var uri = 'XXXXXXXXXX';
var _include_headers = function(body, response, resolveWithFullResponse) {
return {'headers': response.headers, 'data': body};
};
var options = {
method: 'POST',
uri: uri,
json: true,
transform: _include_headers,
body: { "username":"XXXXXXXXXXXXXXX", "password":"XXXXXXXXXX"
},
}
request(options)
.then(function (parsedBody) {
console.log("post success");
})
.catch(function (err) {
console.log("post failed");
});
return request(options)
.then(function(response) {
var result = JSON.stringify(response.headers);
var res = result.match(/{\"authentication-token":".*","content-type/);
stringgy = res[0].substring(25);
authtoken = stringgy.substring(0, stringgy.length-17);
console.log(authtoken);
});
推荐阅读
- python - 如何获取具有相同类的元素的内容
- sql - sqlite - 按现在日期时间过滤数据
- php - Nominatim API 不会通过 CURL 请求返回门牌号,而是在通过浏览器请求时返回
- javascript - Jquery在标签内的输入标签之后获取html或文本
- python - 熊猫如何在组中找到重复的行
- regex - 用空格重新格式化列表字符串
- xquery - 我们如何使用查询找到marklogic中的内存使用情况?
- python - 如何根据任务编写代码
- javascript - 如何强制后处理等到所有 fetch 进程在 node.js 中完成?
- python - python如何解释fstring宽度和零填充?