I want to set cookies in my HTTP POST request.
Something like the cookie field in the HTTP Request below,
version: 0.1.7
Cookie: client=Android; version=
Thanks to Jérémy, I was able to:
Alamofire.request(.POST, url, ...)
.responseJSON {
response in
HTTPClient.updateCookies(response)
...
}
static func updateCookies(response: Response) {
if let
headerFields = response.response?.allHeaderFields as? [String: String],
URL = response.request?.URL {
let cookies = NSHTTPCookie.cookiesWithResponseHeaderFields(headerFields, forURL: URL)
//print(cookies)
// Set the cookies back in our shared instance. They'll be sent back with each subsequent request.
Alamofire.Manager.sharedInstance.session.configuration.HTTPCookieStorage?.setCookies(cookies, forURL: URL, mainDocumentURL: nil)
}
}
You could very likely make this an extension on Request, so the .storeCookies() call would be part of the .validate().responseJSON() chaining.