-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsession.dart
39 lines (33 loc) · 1.03 KB
/
session.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/*
* Based on an answer by Richard Heap on stackoverflow.
* Original link:
* https://stackoverflow.com/questions/50299253/flutter-http-maintain-php-session
*/
import 'dart:async';
import 'package:http/http.dart' as http;
class Session {
static final Session _session = new Session._internal();
factory Session() {
return _session;
}
Session._internal();
Map<String, String> headers = {};
Future<String> get(String url) async {
http.Response response = await http.get(url, headers: headers);
updateCookie(response);
return response.body;
}
Future<String> post(String url, dynamic data) async {
http.Response response = await http.post(url, body: data, headers: headers);
updateCookie(response);
return response.body;
}
void updateCookie(http.Response response) {
String rawCookie = response.headers['set-cookie'];
if (rawCookie != null) {
int index = rawCookie.indexOf(';');
headers['cookie'] =
(index == -1) ? rawCookie : rawCookie.substring(0, index);
}
}
}