【发布时间】:2018-09-24 15:03:29
【问题描述】:
我正在使用 jaguar http 库来执行网络查询。 我正在尝试获取应该从 http 请求返回的 cookie。
我想保存 cookie 并在以后的查询中发送它。
有人可以帮忙吗?
【问题讨论】:
我正在使用 jaguar http 库来执行网络查询。 我正在尝试获取应该从 http 请求返回的 cookie。
我想保存 cookie 并在以后的查询中发送它。
有人可以帮忙吗?
【问题讨论】:
我想通了!
您可以使用他们的拦截器从响应中获取 cookie 并将它们添加到请求中:
以下是关于您的 api 类如何管理 cookie 的建议。
导入'package:jaguar_retrofit/jaguar_retrofit.dart';
import 'package:jaguar_serializer/src/repo/repo.dart';
import 'package:jaguar_resty/jaguar_resty.dart' as resty;
import 'package:jaguar_resty/jaguar_resty.dart';
import 'package:http/http.dart';
import 'package:client_cookie/client_cookie.dart';
@GenApiClient(path: "apiPath")
class MyApi extends _$MyApiClient implements ApiClient{
@override
resty.Route base;
@override
SerializerRepo serializers;
MyApi({this.base, this.serializers}){ //Prepare the API in its constructor
globalClient = IOClient();
List<ClientCookie> cookies = List<ClientCookie>(); //Prepare alist of cookies
this.base.after((resty.StringResponse response){
Map map = response.headers.map((key, value){
return MapEntry(key, value);
});
String cookieStr = map["set-cookie"]; //Extract cookies from response header
print("Cookies Str: $cookieStr");
if(cookieStr != null){
cookies.clear();
cookies.addAll(Utils.cookies(cookieStr)); //Build your cookies and add them to the list
}
}
);
this.base.before((base1){ //This is 'before 'interceptor
base1.cookies(cookies); //add the cookies list to queries here
}
);
}//ApiConstructor
@GetReq(path: "something")
Future<dynamic> getSomething();
}
【讨论】: