Skip to content

Instantly share code, notes, and snippets.

@thosakwe
Created April 18, 2017 20:30
Show Gist options
  • Select an option

  • Save thosakwe/76ce2e37e504d0b36962fbf593bb51c8 to your computer and use it in GitHub Desktop.

Select an option

Save thosakwe/76ce2e37e504d0b36962fbf593bb51c8 to your computer and use it in GitHub Desktop.
Cookies with Dart
import 'dart:convert';
import 'dart:io';
main() async {
var client = new HttpClient();
List<Cookies> sessionCookies = []; // Store our cookies here
// Let's login
var rq = await client.openUrl('POST', 'http://foo.com/login');
rq.write('username=foo&password=bar');
var rs = await rq.close(); // Response from server
sessionCookies.addAll(rs.cookies); // Save copies of all cookies from the server. Usually includes a session id token.
rq = await client.openUrl('GET', 'http://foo.com/protected-resource');
rq.cookies.addAll(sessionCookies); // Send the cookies we've saved
rs = await rq.close();
String body = await rs.transform(UTF8.decoder).join();
print('Protected resource contents: $body');
client.close();
}
@sclee15
Copy link
Copy Markdown

sclee15 commented Apr 20, 2017

Thanks for the useful code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment