Dart HttpClient lost cookies on redirect

787 views
Skip to first unread message

Andrey Zakharov

unread,
Jan 27, 2015, 5:59:06 PM1/27/15
to cl...@dartlang.org
according to http_impl.dart/HttpClientRequest._onIncoming
cookies just getting lost while redirectin

but sometimes servers redirects requests with setting session cookie e.g.

is it issue?

Søren Gjesse

unread,
Jan 28, 2015, 3:22:32 AM1/28/15
to Andrey Zakharov, cl...@dartlang.org
It is true that there is no cookie-jar implementation in the dart:io HttpClient.

If you want to have cookie handling you will have to provide your own cookie handling.

Here is a simple example which just blindle forwards the cookies from the redirect response to the next request. Of course this should check whether the cookies match and the redirect chain could be longer than just one redirect.

import 'dart:async';
import 'dart:io';

var client;

Future<HttpClientResponse> makeRequest(Uri uri, List<Cookie> cookies) {
  return client.openUrl('GET', uri)
      .then((HttpClientRequest request) {
        request.cookies.addAll(cookies);
        request.followRedirects = false;
        return request.close();
      }).then((HttpClientResponse response) {
        return response;
      });
}

main() {
  client = new HttpClient();

  makeRequest(Uri.parse('http://www.google.com'), [])
      .then((HttpClientResponse response) {
        print(response.statusCode);
        print(response.headers);
        print(response.cookies);
        if (response.statusCode == HttpStatus.FOUND) {
          Uri location = Uri.parse(response.headers[HttpHeaders.LOCATION][0]);
          print('Redirecting to $location');
          makeRequest(location, response.cookies)
              .then((HttpClientResponse response) {
                print(response.statusCode);
                print(response.headers);
                print(response.cookies);
              });
          }
      });
}

Same code using async and await (pass --enable-async to the dart executable) - a bit more readable.

import 'dart:async';
import 'dart:io';

var client;

Future<HttpClientResponse> makeRequest(Uri uri, List<Cookie> cookies) async {
  var request = await client.openUrl('GET', uri);
  request.cookies.addAll(cookies);
  request.followRedirects = false;
  return await request.close();
}

main() async {
  client = new HttpClient();

  var response = await makeRequest(Uri.parse('http://www.google.com'), []);
  print(response.statusCode);
  print(response.headers);
  print(response.cookies);
  if (response.statusCode == HttpStatus.FOUND) {
    Uri location = Uri.parse(response.headers[HttpHeaders.LOCATION][0]);
    print('Redirecting to $location');
    response = await makeRequest(location, response.cookies);
    print(response.statusCode);
    print(response.headers);
    print(response.cookies);
  }
}

Regards,
Søren Gjesse

---
Søren Gjesse
Software Engineer, Google Denmark
CVR nr. 28 86 69 84


--
You received this message because you are subscribed to the Google Groups "Dart Server-side and Cloud Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cloud+un...@dartlang.org.
Visit this group at http://groups.google.com/a/dartlang.org/group/cloud/.

Andrey Zakharov

unread,
Jan 28, 2015, 7:55:56 AM1/28/15
to cl...@dartlang.org
Also, it looks like on redirects lost information about proxy (which remains in HttpClient).


среда, 28 января 2015 г., 1:59:06 UTC+3 пользователь Andrey Zakharov написал:

Erol Fox

unread,
Jul 11, 2018, 7:05:35 PM7/11/18
to Dart Server-side and Cloud Development, aazah...@gmail.com

Just wanted to say thanks a million Søren Gjesse !!!
I was struggling for so long, ready to give up on Dart, until I found your post about cookie. No where else have I seen help on 302 that pointed out the need to manually manage cookie.

thanks buddy!!!
Reply all
Reply to author
Forward
0 new messages