求助node的同步http请求中response的cookie获取
发布于 8 年前 作者 smarttang 5420 次浏览 来自 问答
    sync_post_login: function(interFace,url,data){
        var resData,
            results;

        // 设置基本参数
        var options = {
            'json': data,
            'timeout': conf.rep_timeout, // 返回超时
            'socketTimeout': conf.req_timeout // 请求超时
        }

        // 异常包含
        try {
            // 请求过程、返回
            resData = request('POST',url,options);
            if (resData.statusCode == 200){ 
                results = {
                    'data':JSON.parse(resData.getBody('utf8')),
                    'headers':resData.headers
                };
            }else{
                log.reqError(interFace,resData.statusCode,'POST request Status is not Fine!!');
            }
        } catch(e) {
            // 异常处理的地方
            log.reqPackageError(interFace,url,e.stack);
        }
        return results;
    }

在返回值里面,我包含了response中的headers,里面有一个set-cookie,我想的就是获取response返回值里面的cookie,我因该怎么获取? 用的是这个模块:

   var request = require('sync-request');

看了下官方。。貌似也没有说怎么获取返回的session或者cookie值。。。。求助。。。

2 回复

返回的header值是这样的:

{ server: 'nginx/1.10.1',
  date: 'Fri, 07 Oct 2016 11:05:34 GMT',
  'content-type': 'text/html',
  'transfer-encoding': 'chunked',
  connection: 'close',
  'x-powered-by': 'PHP/5.5.31',
  'set-cookie': 
   [ 'ci_session=a%3A5%3A%7Bs%3A10%3A%22session_id%22%3Bs%3A32%3A%220db94f1218de38165748b50b9f6f3399%22%3Bs%3A10%3A%22ip_address%22%3Bs%3A9%3A%22127.0.0.1%22%3Bs%3A10%3A%22user_agent%22%3Bb%3A0%3Bs%3A13%3A%22last_activity%22%3Bi%3A1475838334%3Bs%3A9%3A%22user_data%22%3Bs%3A0%3A%22%22%3B%7D48f584b0cdd9e7ad23151c2c3da0d2a7bb566256; expires=Fri, 07-Oct-2016 13:05:34 GMT; Max-Age=7200; path=/',
     'ci_session=a%3A6%3A%7Bs%3A10%3A%22session_id%22%3Bs%3A32%3A%220db94f1218de38165748b50b9f6f3399%22%3Bs%3A10%3A%22ip_address%22%3Bs%3A9%3A%22127.0.0.1%22%3Bs%3A10%3A%22user_agent%22%3Bb%3A0%3Bs%3A13%3A%22last_activity%22%3Bi%3A1475838334%3Bs%3A9%3A%22user_data%22%3Bs%3A0%3A%22%22%3Bs%3A8%3A%22username%22%3Bb%3A0%3B%7D414dfecf7249b23cf9ece0369e38043fe0755a9d; expires=Fri, 07-Oct-2016 13:05:34 GMT; Max-Age=7200; path=/' ] }

debug请求返回的数据…

Response {
  statusCode: 200,
  headers: 
   { server: 'nginx/1.10.1',
     date: 'Fri, 07 Oct 2016 11:40:52 GMT',
     'content-type': 'text/html',
     'transfer-encoding': 'chunked',
     connection: 'close',
     'x-powered-by': 'PHP/5.5.31',
     'set-cookie': 
      [ 'ci_session=a%3A5%3A%7Bs%3A10%3A%22session_id%22%3Bs%3A32%3A%220d9b0b691614f70a128e402b67bc4fec%22%3Bs%3A10%3A%22ip_address%22%3Bs%3A9%3A%22127.0.0.1%22%3Bs%3A10%3A%22user_agent%22%3Bb%3A0%3Bs%3A13%3A%22last_activity%22%3Bi%3A1475840452%3Bs%3A9%3A%22user_data%22%3Bs%3A0%3A%22%22%3B%7D8587b5ce004acdeec8923956c100ee189d1251ed; expires=Fri, 07-Oct-2016 13:40:52 GMT; Max-Age=7200; path=/',
        'ci_session=a%3A6%3A%7Bs%3A10%3A%22session_id%22%3Bs%3A32%3A%220d9b0b691614f70a128e402b67bc4fec%22%3Bs%3A10%3A%22ip_address%22%3Bs%3A9%3A%22127.0.0.1%22%3Bs%3A10%3A%22user_agent%22%3Bb%3A0%3Bs%3A13%3A%22last_activity%22%3Bi%3A1475840452%3Bs%3A9%3A%22user_data%22%3Bs%3A0%3A%22%22%3Bs%3A8%3A%22username%22%3Bb%3A0%3B%7D62b4be19f18aaeadc1a5d3067fe6db9eba85a5f6; expires=Fri, 07-Oct-2016 13:40:52 GMT; Max-Age=7200; path=/' ] },
  body: <Buffer 7b 22 73 74 61 74 75 73 22 3a 31 7d>,
  url: undefined }
回到顶部