Search code examples
alamofireswift3

Alamofire Swift 3.0 Extra argument in call


I have migrated my project to Swift 3 (and updated Alamofire to latest Swift 3 version with pod 'Alamofire', '~> 4.0' in the Podfile).

I now get an "Extra argument in call" error on every Alamofire.request. Eg:

let patientIdUrl = baseUrl + nextPatientIdUrl
Alamofire.request(.POST, patientIdUrl, parameters: nil, headers: nil, encoding: .JSON)

Can anybody tell me why ?


Solution

  • According to Alamofire documentation for version 4.0.0 URL request with HTTP method would be followings:

    Alamofire.request("https://httpbin.org/get") // method defaults to `.get`    
    Alamofire.request("https://httpbin.org/post", method: .post)
    Alamofire.request("https://httpbin.org/put", method: .put)
    Alamofire.request("https://httpbin.org/delete", method: .delete)
    

    So your url request will be:

    Alamofire.request(patientIdUrl, method: .post, parameters: nil, encoding: JSONEncoding.default, headers: nil)
    

    and a sample request will be:

    Alamofire.request(url, method: .post, parameters: param, encoding: JSONEncoding.default, headers: [AUTH_TOKEN_KEY : AUTH_TOKEN])
        .responseJSON { response in
            print(response.request as Any)  // original URL request
            print(response.response as Any) // URL response
            print(response.result.value as Any)   // result of response serialization
    }
    

    Hope this helps!