AlexP
AlexP

Reputation: 459

iOS Alamofire : read cookie before autoredirect

I have a POST method that on success redirects me on another page. In the final destination as result I have no set Cookies. How to retrieve cookies from first response? (Alamofire does autoredirect)

Alamofire.request( NSLocalizedString("url_login", comment: ""), method: .post, parameters: params).response{ response in          
          if let headerFields = response.response?.allHeaderFields as? [String: String],
           let URL = response.request?.url
            {
                let cookies = HTTPCookie.cookies(withResponseHeaderFields: headerFields, for: URL)
                print(cookies)
            }
            if response.response?.statusCode == 200 {
               /.../
                }else{
                    /..../
                }
            }

Upvotes: 1

Views: 286

Answers (1)

Vyacheslav
Vyacheslav

Reputation: 27221

Just disable redirect and read the results. like this:

delegate.taskWillPerformHTTPRedirection = { _, _, _, _ in return nil }

https://github.com/Alamofire/Alamofire/blob/491cf2f2557379d212eca2ed3e62f01a50c8c1b4/Tests/SessionDelegateTests.swift

Upvotes: 1

Related Questions