How to get response headers when using Alamofire in Swift?

前端 未结 3 1661
时光说笑
时光说笑 2021-01-02 00:47

I\'m using Alamofire for my Rest (POST) request and getting JSON response seamlessly. But i can access only response body. I want to get response headers. Isn\'t it possible

3条回答
  •  攒了一身酷
    2021-01-02 01:20

    This code gets response header in Swift 4.2

    Alamofire.request(pageUrlStr, method: .post, parameters: Parameter, encoding: URLEncoding.httpBody, headers: nil).responseJSON
                { response in
                    //to get JSON return value
                    if let ALLheader = response.response?.allHeaderFields  {
                        if let header = ALLheader as? [String : Any] {
                            if let cookies = header["Set-Cookie"] as? String {
                                UserDefaults.standard.set(cookies, forKey: "Cookie")
                            }
                        }
                    }
    }
    

提交回复
热议问题