How to set Cookies in alamofire?

Viewed 18298

How to set Cookies in Alamofire such that such that every time I kill the app and restart it, the same cookie is sent?

5 Answers

Swift 5.1 & Alamofire 5.0

Prepare your cookies

let cookieProps = [
    HTTPCookiePropertyKey.domain: "##put your domain here##",
    HTTPCookiePropertyKey.path: "/",
    HTTPCookiePropertyKey.name: "##put your cookie key here##",
    HTTPCookiePropertyKey.value: "##put your cookie value here##"
   ]

Set your cookies

if let cookie = HTTPCookie(properties: cookieProps) {
    AF.session.configuration.httpCookieStorage?.setCookie(cookie)
}

After that, you can do your normal request and the cookies will be sent to server

Alamofire.request(
  ##URL##,
  method: ##.post or .get##,
  parameters: parameters
  ).responseString {
    response in 
    ....
}

Related