访问nodejs中先前帖子返回的标头

时间:2018-12-03 16:15:44

标签: node.js request request-promise

我正在node.js中进行POST。从那里,我需要访问一个身份验证令牌标头以使用该身份验证令牌作为标头值进行另一个POST。

我在从返回的对象中获取身份验证令牌时遇到问题。

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);



});

我得到的输出如下。我尝试将其转换为字符串,并访问response.headers [0] .authentication-token,但是对于如下所示的密钥对,返回的错误未定义。

    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)

1 个答案:

答案 0 :(得分:0)

我最终使用了它。

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);
});