Swift 2.0, Alamofire: Set cookies in HTTP Post Request
Asked Answered
N

5

11

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=0.1.7; sellerId=SEL5483318784; key=178a0506-0639-4659-9495-67e5dffa42de
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Content-Length: 1431

How can I achieve this with Alamofire?

My current Alamofire request is like this,

Alamofire.request(.POST, ServerConfig.ADD_PRODUCT_URL, parameters: productJSON, encoding: .JSON, headers: nil)
     .responseJSON(completionHandler: { responseRequest, responseResponse, responseResult in
         print(responseRequest!.URL)
         print(responseResponse)
         print(responseResult)
     })
Nurserymaid answered 12/10, 2015 at 14:6 Comment(0)
M
19

I had the same problem on a project and I do something like this to solve it:

let cookies = NSHTTPCookie.cookiesWithResponseHeaderFields(response.allHeaderFields as! [String: String], forURL: response.URL!)
Alamofire.Manager.sharedInstance.session.configuration.HTTPCookieStorage?.setCookies(cookies, forURL: response.URL!, mainDocumentURL: nil)

You just have to do this one time, because the Alamofire instance is a singleton, so for all the next request the cookie is set.

Hope it's what you are looking for :)

Maganmagana answered 19/10, 2015 at 17:36 Comment(0)
T
2

Swift 3:

I had an array of cookies saved in my UserDefaults and what I did to attach them to the request was:

var request = URLRequest(url: "https://yourURL.com")
if let cookies = cookies as? [HTTPCookie] {
   let headers = HTTPCookie.requestHeaderFields(with: cookies)
   request.allHTTPHeaderFields = headers
}
Tarantella answered 13/3, 2017 at 12:33 Comment(0)
L
1

Thanks to Jérémy, I was able to:

Alamofire.request(.POST, url, ...)
    .responseJSON { 
    response in 
    HTTPClient.updateCookies(response)
    ...
}

static func updateCookies(response: Response<AnyObject, NSError>) {
    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.

Leanora answered 15/3, 2016 at 19:44 Comment(0)
N
1
swift 5 and Xcode 11
let cookies = HTTPCookie.cookies(withResponseHeaderFields: response.response?.allHeaderFields as! [String: String], for: (response.response?.url)!)

Alamofire.SessionManager.default.session.configuration.httpCookieStorage?.setCookies(cookies, for: response.response?.url, mainDocumentURL: nil)
Neural answered 20/2, 2021 at 0:6 Comment(0)
B
0

iOS 11 & Alamofire 4

let webDataStore = webView.configuration.websiteDataStore
webDataStore.httpCookieStore.getAllCookies { (cookies) in
    Alamofire.SessionManager.default.session.configuration.httpCookieStorage?.setCookies(
        cookies,
        for: url,
        mainDocumentURL: nil
    )
}
Bibliotaph answered 7/5, 2018 at 21:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.