I'd like to make an http request to a remote server while properly handling cookies (eg. storing cookies sent by the server, and sending those cookies when I make subsequent requests). It'd be nice to preserve any and all cookies
for http request I am using
static Future<Map> postData(Map data) async {
http.Response res = await http.post(url, body: data); // post api call
Map data = JSON.decode(res.body);
return data;
}
I've published a small flutter library called requests to assist with cookie-aware http requests.
Usage:
find out more about requests
Here's an example of how to grab a session cookie and return it on subsequent requests. You could easily adapt it to return multiple cookies. Make a
Session
class and route all yourGET
s andPOST
s through it.I have improved the Richard Heap's solution to be capable to process multiple 'Set-cookies' and multiple cookies.
In my case, the server returns multiples 'Set-cookies'. The http package concatenate all the set-cookies headers in one header and separate it by comma (',').