0

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

shakti goyal
  • 161
  • 1
  • 1
  • 7

1 Answers1

1
var response = await http.get(private_api_url, headers: {'cookie': cookie});
Patrick
  • 3,578
  • 5
  • 31
  • 53
  • Worked perfectly! One question though, to keep the session alive, do I need to store the cookie in sharedPreferences? or would I need to relogin whenever I restart the app? – shakti goyal Feb 16 '23 at 19:54
  • Or maybe you can use the WebviewCookieManager for this? (I didn't try.) – Patrick Feb 17 '23 at 08:05