【问题标题】:node js express-session Set-Cookie on Postman/ Not in Flutter http headers节点 js express-session Set-Cookie on Postman/ 不在 Flutter http 标头中
【发布时间】:2021-01-26 03:54:55
【问题描述】:

我在尝试使用 express-session 将颤振应用程序与我的 nodejs 后端连接时遇到问题。在邮递员中,响应标头包含一个“Set-Cookie”-Header,但带有http.post(...) 的颤动标头不包含:标头:{content-length: 113, content-type: application/json; charset=utf-8}。

我需要一个 cookie 来保持经过身份验证的会话与护照。任何想法如何解决它?

颤振标题: host: '127.0.0.1:3000', connection: 'keep-alive', 'content-length': '57', 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.75 Safari/537.36', 'content-type': 'application/json; charset=utf-8', accept: '*/*', origin: 'http://localhost:51879', 'sec-fetch-site': 'cross-site', 'sec-fetch-mode': 'cors', 'sec-fetch-dest': 'empty', referer: 'http://localhost:51879/', 'accept-encoding': 'gzip, deflate, br', 'accept-language': 'de-DE,de;q=0.9,en-US;q=0.8,en;q=0.7'

邮递员标头:'content-type': 'application/json', accept: '*/*', 'postman-token': '7c79280d-****-****-a985-c01395e50e08', host: 'localhost:3000', 'accept-encoding': 'gzip, deflate, br', connection: 'keep-alive', 'content-length': '66'

【问题讨论】:

    标签: node.js flutter session header session-cookies


    【解决方案1】:

    建议您使用dio library 进行 HTTP 调用。

    import 'package:dio/dio.dart';
    
    class ApiProvider {
      Dio _dio;
      String aToken = '';
    
      final BaseOptions options = new BaseOptions(
        // base url to backend server
        baseUrl: 'http://a.b.c.d:port/',  
        connectTimeout: 15000,
        receiveTimeout: 13000,
      );
      static final ApiProvider _instance = ApiProvider._internal();
    
      factory ApiProvider() => _instance;
    
      ApiProvider._internal() {
        _dio = Dio(options);
        _dio.interceptors.add(InterceptorsWrapper(
            onRequest:(Options options) async {
              // to prevent other request enter this interceptor, 
              // use a new Dio(to avoid dead lock) instance to request token.
              _dio.interceptors.requestLock.lock();
              
              // set the cookie to headers
              options.headers["cookie"] = aToken;
    
              _dio.interceptors.requestLock.unlock();
              return options; // continue
            }
        ));
      }
    
      Future login() async {
        final request = {
          "userName": "",
          "password": "",
          "token": ""
        };
        final response = await _dio.post('/login', data: request, options: Options(
            followRedirects: false,
            validateStatus: (status) { return status < 500; }
        ));
        //get cooking from response
        final cookies = response.headers.map['set-cookie'];
        if (cookies.isNotEmpty && cookies.length == 2) {
          // it depends on how your server sending cookie
          aToken = cookies[1].split(';')[0];
        }
      }
    
      /// if we call this function without cookie then it will throw 500 err.
      Future getSomething() async {
        final response = await _dio.post('/something');
      }
    }
    

    【讨论】:

    • 我以前用过这个库。我在登录响应后添加了print(response.headers);,也没有“Set-Cookie”。我再次用邮递员对其进行了测试,并在标题中设置了一个 cookie。可能是因为我发送到服务器的标头吗?还是在本地主机上?
    • 很遗憾没有。响应标头仍然不包含“Set-Cookie”
    • 将followRedirects: false 和validateStatus: (status) { return status &lt; 500;} 添加到选项中。检查更新的答案。
    • 它也不起作用。也许它取决于本地主机?你有什么想法吗?
    猜你喜欢
    • 1970-01-01
    • 2012-12-07
    • 1970-01-01
    • 2013-02-08
    • 1970-01-01
    • 1970-01-01
    • 2016-07-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多