Search code examples
iosswiftalamofire

Alamofire 5.1 update issue in post method HTTPHeader, network call using alamofire


I'm using the Api manager for generic network calls, Just get the instance of Api Manager and access the pos or get function defined in Api Manger class. Now I update my pod Alamofire 4.7 to Alamofire 5.1. Facing the issue, Issue is that in older version HTTPHeader type was String:String So I create a dictionary in a controller and pass to function. But now I need to add custom HTTPHeader e.g let header: HTTPHeader = ["abc":"xyz"] But its not possible to do in every class.

API manger Function

        func postRequest (urlString: String!, isAlertShow: Bool, parameters: Parameters, header : HTTPHeaders, successCallback : @escaping (Data) -> Void, errorCallBack : @escaping (String) -> Void) -> Void {
    
    if !Reachability.isConnectedToNetwork() {
        print("Internet Connection not Available!")
        errorCallBack("Internet Connection not Available!")
        return
    } else {
        print("Internet Connection Available!")
    }
    if isAlertShow {
        SKActivityIndicator.show("Loading...", userInteractionStatus: false)
    }
    
    print(header)
    print(urlString!)
    print(parameters)
    Alamofire.request(urlString, method: .post, parameters: parameters, encoding: JSONEncoding.default, headers: header).responseJSON {response in
        print(response)
        
        SKActivityIndicator.dismiss()
        
        if let JSON = response.result.value as? [String: Any] {
            
            let statusCode = response.response?.statusCode
            
            if statusCode == 200 {
                successCallback(response.data!)
            } else {
                let errorMsg = JSON["error"]
                if errorMsg != nil {
                    errorCallBack(errorMsg as! String)
                } else {
                    let errotxt = JSON["message"]
                    errorCallBack(errotxt as! String)
                }
            }
        } else {
            errorCallBack(MSG)
        }
    }
}

View Controller where I call Api Function

    var roDictionary = [String:String]()
    var bodyDict = [String:Any]()
    ApiManager.shared.postRequest(urlString: BASE_URL + SUBMIT_RO, isAlertShow: true, parameters: bodyDict, header: roDictionary, successCallback: { (response) in
           // Get response as Data
       }, errorCallBack: { (error) in
           print (error)
           self.showAlert(title: ALERT, message: error)
       })

So now how I can pass dictionary as HTTPHeader for Alamofire 5.1, And what about this change response.result.value Please anybody change my Api Manger post meth if else into Switch according to my implementation.


Solution

  • I think for Alamofire 5.x versions if you want to add a dictionary for your HTTPHeaders, you can use their init with dictionary method.

    HTTPHeaders.init(dictionary: [String : String])
    

    Here you can pass your dictionary. It has several other initializers like

    HTTPHeaders.init(headers: [HTTPHeader])
    

    where you can create an HTTPHeader specifically and populate with array of HTTPHeader