1

currently this code is fetching data from the API but i am trying to figure out that how to send cookie key value pair with this api call...

as a newbie i am not able to figure out the solution any help will greatly appreciated..

Here is my code..

Future getdata(String url) async{
    var data;

    final response = await http.get(Uri.parse(url));

    if(response.statusCode == 200 || response.statusCode == 400){
      data = convert.jsonDecode(response.body);
    } else {
      throw Exception('Failed to connect with server');
    }

    return data;
  }

here is a cookie value -

 mid YGgv5AABAAEfUwpKKr_NQd6SCBin
ig_did 1A1B3933-7011-48B8-BA04-C19C769E91B1
ig_nrcb 1
shbid 6017
shbts 1617479398.7656476
rur NAO
csrftoken xKPVsGdKuHuffJDrgCMFHxhQPI1agWQf
ds_user_id 916506255
sessionid 9165056255%3MdvxY9kiMXXyR%3A15

I stored every keyvalue pair in database ...i can fetch any value with its key name

Nalin Nishant
  • 716
  • 1
  • 5
  • 20
  • you can check this : https://stackoverflow.com/questions/52241089/how-do-i-make-an-http-request-using-cookies-on-flutter – Fahmi Sawalha Apr 04 '21 at 14:24
  • i have some error while implementing this with my above code.. can you please give me a example... as i already have a stored cookie info in db as a key value pair – Nalin Nishant Apr 04 '21 at 14:29

1 Answers1

1

After reading official docs i am able to set cookies with my api req

import 'dart:async';
import 'package:hive/hive.dart';
import 'dart:convert' as convert;
import 'package:http/http.dart' as http;

class InstaController{
  late Box dbcookie;

  Future getdata(String url) async{
    var data;
    Map<String, String> headers = {};

    dbcookie = Hive.box('dbcookie');
    headers["mid"] = dbcookie.get('mid');
    headers["ig_did"] = dbcookie.get('ig_did');
    headers["ig_nrcb"] = dbcookie.get('ig_nrcb');
    headers["shbid"] = dbcookie.get('shbid');
    headers["shbts"] = dbcookie.get('shbts');
    headers["rur"] = dbcookie.get('rur');
    headers["csrftoken"] = dbcookie.get('csrftoken');
    headers["ds_user_id"] = dbcookie.get('ds_user_id');
    headers["sessionid"] = dbcookie.get('sessionid');

    final response = await http.get(Uri.parse(url), headers: headers);

    if(response.statusCode == 200 || response.statusCode == 400){
      data = convert.jsonDecode(response.body);
    } else {
      throw Exception('Failed to connect with server');
    }

    return data;
  }
}
Nalin Nishant
  • 716
  • 1
  • 5
  • 20