Swift 2.0, Alamofire: Set cookies in HTTP Post Request

前端 未结 4 558
长情又很酷
长情又很酷 2021-02-14 00:48

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=         


        
4条回答
  •  滥情空心
    2021-02-14 01:23

    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 :)

提交回复
热议问题