1

I am trying to fetch data after successful login. I have cookie based auth on backend. I checked the login response and I see that the cookie is present in the headers. I am not sure why my requests are not going with cookie.

I keep getting this in my console, code for not authorized. I/flutter (21293): 401

class Session {
  Map<String, String> headers = {};

  Future<Map> get(String endpoint) async {
    http.Response response = await http.get(Uri.parse("$baseUrl$endpoint"), headers: headers);
    print(response.statusCode);
    updateCookie(response);
    return jsonDecode(response.body);
  }

  Future<http.Response> post(dynamic data, String endpoint) async {
    http.Response response = await http.post(Uri.parse("$baseUrl$endpoint"), body: json.decode(data), headers: headers);
    updateCookie(response);
    return response;
  }

  void updateCookie(http.Response response) {
    String? rawCookie = response.headers['set-cookie'];
    if (rawCookie != null) {
      int index = rawCookie.indexOf(';');
      headers['cookie'] =
      (index == -1) ? rawCookie : rawCookie.substring(0, index);
    }
  }
}

Tried to print rawCookie as well

I/flutter (21293): _api_key=SFMyNTY.g3QAAAABbQAAAAhpZGVudGl0eXQAAAAHZAAKX19zdHJ1Y3RfX2QAF0VsaXhpci5HYXRld2F5LklkZW50aXR5ZAAGYWN0aXZlZAAEdHJ1ZWQACWF2YXRhcl9pZG0AAAAkNTY4NWMwNTMtYThhMS00MDA5LWJhN2UtZmJkNTkyMjBhM2U1ZAAHY291bnRyeXQAAAAGZAAEZmxhZ20AAAAI8J-HtfCfh7FkAAppc29fbmFtZV8ybQAAAAJwbGQACmlzb19uYW1lXzNtAAAAA3BvbGQABG5hbWVtAAAABlBvbGFuZGQACG51bV9jb2RlbQAAAAM2MTZkAAVwb2ludHQAAAAEZAAKX19zdHJ1Y3RfX2QAEEVsaXhpci5HZW8uUG9pbnRkAAtjb29yZGluYXRlc2gCYRRhNGQACnByb3BlcnRpZXN0AAAAAGQABHNyaWRiAAAQ5mQAC2Rlc2NyaXB0aW9ubQAAAAF4ZAACaWRtAAAAJGM2ZTljY2Q0LTM4MmItNDEzZi04ODYyLTc2ZjM5ZTYxOGFiNGQABG5pY2ttAAAACHRlc3Rzc3Nz.80iQK3sUwPPVj1pkaZsKgMxQ4Lt8aW8-ndYbPSucGag; path=/; HttpOnly
I/flutter (21293): 200

Then I use it

class Items{
  Future<Map> fetchItems() async {
    final response = await Session().get("/user/items");
    return response;
  }
}
Dave
  • 1
  • 1
  • 9
  • 38
  • You can check https://stackoverflow.com/questions/52241089/how-do-i-make-an-http-request-using-cookies-on-flutter . I think that `rawCookie` is not proper parsed in your code. – Alan haha Dec 24 '21 at 02:29

1 Answers1

1

I had the same problem. I resolved it by using dio and cookiejar instead of HTTP.

Add these dependencies in your pubspec.yaml:

dependencies:
  dio: ^4.0.4
  dio_cookie_manager: ^2.0.0
  cookie_jar: ^3.0.1

Here's an example to use dio:

var dio =  Dio(BaseOptions(
      connectTimeout: 10000,  // in ms
      receiveTimeout: 10000,
      sendTimeout: 10000,
      responseType: ResponseType.plain,
      followRedirects: false,
      validateStatus: (status) { return true; }
  ));   // some dio configurations

dio.interceptors.add(CookieManager(CookieJar()));

Response response = await dio.post(
          "http://example.com/login",
          data: FormData.fromMap(
             {
                'username': 'myUser',
                'password': 'myPassword',
             }
          ));  // cookies are automatically saved


Response nextResponse = await dio.post("http://example.com/user/items");

Hzzkygcs
  • 1,532
  • 2
  • 16
  • 24
  • do I need to to write dio.interceptors.add.. with every request? I still keep getting 401 with your example :/ – Dave Dec 24 '21 at 17:08
  • To keep the cookie, you have to use the same `Dio(BaseOptions())`. You might want to keep it in a global variable or something. You only need to call `dio.interceptors.add` once for every `Dio(BaseOptions())` instantiation. You might also want to consider `PersistCookieJar` if you want persistent cookies (stored in non-volatile storage instead of RAM). – Hzzkygcs Dec 24 '21 at 18:13
  • @squnk How's your problem? Has it been solved yet? I just realized that dio and cookie jar didn't work when you run your flutter in chrome, but it will run very well if you run it in android emulator – Hzzkygcs Dec 29 '21 at 10:13
  • it been solved, the issue was I was creating creating new Dio instance instead of using only one. – Dave Dec 29 '21 at 11:39
  • I see. I'm glad to hear that! – Hzzkygcs Dec 29 '21 at 15:53