首页 > 解决方案 > 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)

标签: node.jsrequestrequest-promise

解决方案


我最终使用了这个。

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

推荐阅读