Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I add multiple headers in Flutter GET Api request

how can I add set-cookie in Headers of Flutter API. I've used the following dart plugin:

Dart Plugin

I am following these links but couldn't add headers.

Flutter Cookbook

Stackoverflow Question

Below is my Code:

Future<Map> getApi() async {

    Map<String, String> headers = {};

    // HEADERS TO BE ADDED
    headers['Set-Cookie'] = "user_image=; Path=/";
    headers['Set-Cookie'] = "user_id=Administrator; Path=/";
    headers['Set-Cookie'] = "system_user=yes; Path=/";
    headers['Set-Cookie'] = "full_name=Administrator; Path=/";
    headers['Set-Cookie'] = "sid=123456789; Expires=Mon, 25-Feb-2019 11:01:39 GMT; Path=/";

    // API RESPONSE
    http.Response response = await http.get(
      apiUrl,
      headers: headers,
    );

    // CONVERT TO JSON AND MAP
    Map responseBody = convert.jsonDecode(response.body);

    return responseBody;
  }
like image 849
Zain SMJ Avatar asked Mar 20 '26 03:03

Zain SMJ


1 Answers

With the help of the solution given on this link I was able to solve my issue. Below is the HTTP request to add headers:

http.Response response = await http.get(
   apiUrl,
   headers: {'Cookie': 'sid=123456789'},
);

Thanks for the help guys.

like image 167
Zain SMJ Avatar answered Mar 22 '26 17:03

Zain SMJ



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!