Node.js node js express会话在Postman上设置Cookie,而不是在Flatter http头中设置Cookie
我在尝试使用express session将颤振应用程序与我的nodejs后端连接时遇到了一个问题。在postman中,响应头包括一个“Set Cookie”头,但是带有Node.js node js express会话在Postman上设置Cookie,而不是在Flatter http头中设置Cookie,node.js,flutter,session,header,session-cookies,Node.js,Flutter,Session,Header,Session Cookies,我在尝试使用express session将颤振应用程序与我的nodejs后端连接时遇到了一个问题。在postman中,响应头包括一个“Set Cookie”头,但是带有http.post(…)的flift头不包括:头:{content length:113,content type:application/json;charset=utf-8} 我需要一个cookie来保持passport的身份验证会话。有没有办法解决这个问题 颤振头: host:'127.0.0.1:3000',connec
http.post(…)
的flift头不包括:头:{content length:113,content type:application/json;charset=utf-8}
我需要一个cookie来保持passport的身份验证会话。有没有办法解决这个问题
颤振头:
host:'127.0.0.1:3000',connection:'keep alive','content length':'57','user agent':'Mozilla/5.0(windowsnt 10.0;Win64;x64)AppleWebKit/537.36(KHTML,类似Gecko)Chrome/86.0.4240.75 Safari/537.36','content type':'application/json;字符集=utf-8',接受:'*/*',原点:'http://localhost:51879“,”sec fetch site“:”跨站点“,”sec fetch mode“:”cors“,”sec fetch dest“:”空“,referer:”http://localhost:51879/“,”接受编码“:”gzip,deflate,br“,”接受语言“:”de de,de;q=0.9,在美国;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'
建议您用于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');
}
}
import'包:dio/dio.dart';
类API提供程序{
迪奥迪奥;
字符串aToken='';
最终基本选项=新的基本选项(
//后端服务器的基本url
baseUrl:'http://a.b.c.d:port/',
连接超时:15000,
接收超时:13000,
);
静态最终ApiProvider _instance=ApiProvider._internal();
工厂ApiProvider()=>\u实例;
ApiProvider.\u internal(){
_dio=dio(选项);
_添加(拦截器包装器)(
onRequest:(选项)异步{
//为了防止其他请求进入此拦截器,
//使用新的Dio(以避免死锁)实例来请求令牌。
_interceptors.requestLock.lock();
//将cookie设置为headers
options.headers[“cookie”]=aToken;
_interceptors.requestLock.unlock();
返回选项;//继续
}
));
}
Future login()异步{
最终请求={
“用户名”:“”,
“密码”:“,
“令牌”:”
};
最终响应=等待发布('/login',数据:请求,选项:选项(
followRedirects:false,
validateStatus:(状态){返回状态<500;}
));
//从回应中获得烹饪
final cookies=response.headers.map['set-cookie'];
if(cookies.isNotEmpty&&cookies.length==2){
//这取决于服务器发送cookie的方式
aToken=cookies[1]。拆分(“;”)[0];
}
}
///如果我们在没有cookie的情况下调用这个函数,那么它将抛出500个错误。
Future getSomething()异步{
最终响应=等待发布('/something');
}
}
我以前用过这个库。我添加了print(response.headers)代码>在登录响应之后,并且也没有“设置Cookie”。我和邮递员再次测试了一下,在信头上放了一块饼干。可能是因为我发送到服务器的头?或者在本地主机上?不幸的是不是。响应头仍然不包含“Set Cookie”,将followRedirects:false
和validateStatus:(status){return status<500;}
添加到选项中。检查更新的答案。它也不起作用。也许这取决于本地主机?你知道吗?