flutterdartflutter-httpflutterwebviewplugin

How to pass cookie in http request - Flutter


What I'm trying do is access the Instagram private api which requires user to be logged in before sending request to it, so to achieve that, I accessed the cookies or session id with the help of flutter_web_view and webview_cookie_manager (basically making the user logged in through webview and then get the cookie using cookie_manager package)

But how to pass that cookie in http request?

// accessing cookie or session id
 
  WebViewController webViewController = WebViewController()
    ..loadRequest(Uri.parse('https://www.instagram.com/'))
    ..setJavaScriptMode(JavaScriptMode.unrestricted)
    ..setNavigationDelegate(
      NavigationDelegate(
        onPageFinished: (value) async {
          final cookieManager = WebviewCookieManager();
          List<Cookie> gotCookies = await cookieManager.getCookies('https://www.instagram.com/');
          print(gotCookies); // recieved the cookies
        }
      )
    )
  ;

var response = await http.get(private_api_url}); // how to pass cookies in this request


Solution

  • var response = await http.get(private_api_url, headers: {'cookie': cookie});