I'm currently trying to retrieve multiple Set-Cookie fields from a GET or POST request using Dio. I have not been able to do so using either HttpClient or flutter's http.

Using Dio throws an exception:

HttpException: More than one value for header set-cookie

and I want to know how to get around that and handle multiple set-cookie headers, and then send them back in a cookie header.

How to deal with multiple Set-Cookie headers and send them back as a cookie header using Dart/Flutter?

Here is the MCVE

Dio dio = new Dio();

dio.get(urlLogin).then((Response resp){
  print('-----Login-----');
  print(resp.headers.value('set-cookie'));
});

And the StackTrace

HttpException: More than one value for header set-cookie
#0      _HttpHeaders.value (dart:_http/http_headers.dart:48:7)
#1      loginDio.<anonymous closure> (file:///home/fuguet/Prog/Dart/FPlogin/main.dart:55:24)
#2      _RootZone.runUnary (dart:async/zone.dart:1379:54)
#3      _FutureListener.handleValue (dart:async/future_impl.dart:129:18)
#4      Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:642:45)
#5      Future._propagateToListeners (dart:async/future_impl.dart:671:32)
#6      Future._complete (dart:async/future_impl.dart:476:7)
#7      _SyncCompleter.complete (dart:async/future_impl.dart:51:12)
#8      _RootZone.runUnary (dart:async/zone.dart:1379:54)
#9      _FutureListener.handleValue (dart:async/future_impl.dart:129:18)
#10     Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:642:45)
#11     Future._propagateToListeners (dart:async/future_impl.dart:671:32)
#12     Future._complete (dart:async/future_impl.dart:476:7)
#13     _SyncCompleter.complete (dart:async/future_impl.dart:51:12)
#14     _RootZone.runUnary (dart:async/zone.dart:1379:54)
#15     _FutureListener.handleValue (dart:async/future_impl.dart:129:18)
#16     Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:642:45)
#17     Future._propagateToListeners (dart:async/future_impl.dart:671:32)
#18     Future._completeWithValue (dart:async/future_impl.dart:486:5)
#19     Future._asyncComplete.<anonymous closure> (dart:async/future_impl.dart:516:7)
#20     _microtaskLoop (dart:async/schedule_microtask.dart:41:21)
#21     _startMicrotaskLoop (dart:async/schedule_microtask.dart:50:5)
#22     _Timer._runTimers (dart:isolate/runtime/libtimer_impl.dart:391:30)
#23     _Timer._handleMessage (dart:isolate/runtime/libtimer_impl.dart:416:5)
#24     _RawReceivePortImpl._handleMessage (dart:isolate/runtime/libisolate_patch.dart:165:12)


Solution 1: Richard Heap

As there could be more than one header with the same name, you can't use value(). Instead use [] to retrieve the List<String>.

List<String> rawCookies = resp.headers['set-cookie'];