Flutter Jaguar HTTP Cookie

时间:2018-09-24 15:03:29

标签: dart flutter

我正在使用Jaguar http库执行网络查询。 我正在尝试获取应从http请求返回的cookie。

我想保存cookie并在以后的查询中发送它。

任何人都可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

我知道了!

您可以使用其拦截器从响应中获取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();
}