简体   繁体   English

节点 js express-session Set-Cookie on Postman/不在 Flutter http 标头中

[英]node js express-session Set-Cookie on Postman/ Not in Flutter http headers

I'm facing an issue trying to connect an flutter application with my nodejs backend with express-session.我在尝试使用 express-session 将 Flutter 应用程序与我的 nodejs 后端连接时遇到问题。 In postman the response header includes a "Set-Cookie"-Header, but the flutter headers with http.post(...) do not: headers: {content-length: 113, content-type: application/json; charset=utf-8}在邮递员中,响应头包含一个“Set-Cookie”-Header,但带有http.post(...)的颤动头不包括: headers: {content-length: 113, content-type: application/json; charset=utf-8} {content-length: 113, content-type: application/json; charset=utf-8} . {content-length: 113, content-type: application/json; charset=utf-8}

I need a cookie to keep the authenticated session with passport.我需要一个 cookie 来保持经过身份验证的会话与护照。 Any ideas how to fix it?任何想法如何解决它?

Flutter headers: 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' Flutter 标头: 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' 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'

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

Recommend you to use dio library for HTTP calls.建议您使用dio 库进行 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');
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Node JS,快速会话,从客户端浏览器中删除 cookie - Node JS, express-session, remove cookie from client browser Express-session 没有设置cookie? - Express-session does not set cookie? Heroku 上的节点 API 发送 set-cookie 标头,但 Chrome 不会设置它们(但是 Postman 会) - Node API on Heroku sends set-cookie headers, but Chrome won't set them (Postman, however, will) 未设置 Cookie,即使它位于响应标头中。 使用快速会话 cookies - Cookie not set, even though it is in response headers. Using express-session cookies Node.js 快速会话在 HTTP 上正常,session 未在 Z0E8433F9A404F1F3BA21Z01C 中恢复 - Node.js express-session ok on HTTP, session not recovered in HTTPS, localhost, self-signed certificate res.setHeader(“ Set-Cookie”,…)未在Node / Express中设置cookie - res.setHeader(“Set-Cookie”, …) is not setting the cookie in Node / Express Express:禁用特定路由的标头(Etag、set-cookie 等) - Express: Disable headers for specific routes (Etag, set-cookie, etc) 快速会话不会设置cookie connect.sid - express-session won't set cookie connect.sid express-session 无法在生产环境中设置 connect.sid cookie - express-session unable to set connect.sid cookie in production 快速会话和护照发送后无法设置标题 - express-session and passport Can't set headers after they are sent
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM