I'm trying to make my Alamofire manager instance automatically remember & set cookies, here is my unsuccessful attempt :
let cfg = NSURLSessionConfiguration.defaultSessionConfiguration()
let cooks = NSHTTPCookieStorage.sharedHTTPCookieStorage()
// makes no difference whether it's set or left at default
cfg.HTTPCookieStorage = cooks
cfg.HTTPCookieAcceptPolicy = NSHTTPCookieAcceptPolicy.Always
let mgr = Alamofire.Manager(configuration: cfg)
mgr.request(NSURLRequest(URL: NSURL(string: "http://httpbin.org/cookies/set?stack=overflow"))).responseString {
(_, _, response, _) in
var resp = response // { "cookies": { "stack": "overflow" } }
// becomes empty if cfg.HTTPCookieStorage set to nil
}
mgr.request(NSURLRequest(URL: NSURL(string: "http://httpbin.org/cookies"))).responseString {
(_, _, response, _) in
var resp = response // { "cookies": {} }
// always empty no matter what
}
cooks.cookiesForURL(NSURL(string: "http://httpbin.org/cookies")) // 0 elements
The first URL sends a Set-Cookie : stack=overflow; Path=/
header and then redirects (302) to /cookies
(equivalent of the second URL); this works fine in my browser (once I hit the first URL the second URL always display that cookie) so I'd like to replicate that behavior with Alamofire.