Swift 2.0, Alamofire: Set cookies in HTTP Post Request

前端 未结 4 559
长情又很酷
长情又很酷 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:30

    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
    }
    

提交回复
热议问题