How do I can iterate cookie values from http response?

吃可爱长大的小学妹 提交于 2021-01-29 19:17:04

问题


I am using rest API in my flutter app. For further request I need JSESSIONID which I received from my profile API. I successful got response but I need guide to iterate cookie value.

I followed following steps:

final response = await http.get(
      strURL,
      headers: {
        "Authorization": basicAuth,
        "Content-Type": "application/json"
      },
    );

    String rawCookie = response.headers['set-cookie'];
    print('rawCookie $rawCookie');

As print raw cookie it is printing details:

flutter: rawCookie __cfduid=d5bbe3f8a131478a78ae996e636cca0401544177738; expires=Sat, 07-Dec-19 10:15:38 GMT; path=/; domain=.rayz.ch; HttpOnly,JSESSIONID=6AD6698C5BFC90F1D089696A955E6824; Path=/; HttpOnly

I can iterate it by substring but I want to iterate it with a proper way. So please guide me on this.


回答1:


With package:http you need to split the cookie string yourself using String.split. If you want to use the underlying http client, that gives you a pre-parsed list of cookies, for example:

  HttpClient _httpClient = new HttpClient();
  HttpClientRequest request = await _httpClient.postUrl(Uri.parse(url));
  request.headers.set('content-type', 'application/json');
  request.add(utf8.encode(json.encode(jsonMap)));
  HttpClientResponse response = await request.close();
  print(response.cookies); // this is a List<Cookie>


来源:https://stackoverflow.com/questions/53667571/how-do-i-can-iterate-cookie-values-from-http-response

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!