Alamofire Swift 3.0 Extra argument in call

AlamofireSwift3

Alamofire Problem Overview


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 ?

Alamofire Solutions


Solution 1 - Alamofire

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!

Solution 2 - Alamofire

This one worked for me.
No need to remove encoding parameter

Update for Swift 5.x

Alamofire uses the Result type introduced in Swift 5.
Also Alamofire.request has been changed to AF.request which will now read their switch response.result value with .success and .failure

AF.request("https://yourServiceURL.com", method: .get, parameters: [:], encoding: URLEncoding.default, headers: ["":""]).responseJSON { (response) in
        switch response.result {
        case let .success(value):
            print(value)
        case let .failure(error):
            print(error)
    }
}

Swift 3.x / 4.x

Alamofire.request("https://yourServiceURL.com", method: .post, parameters: parameters, encoding: JSONEncoding.default, headers: nil).responseJSON { (response:DataResponse<Any>) in
        
        switch(response.result) {
        case .success(_):
            if let data = response.result.value{
                print(response.result.value)
            }
            break
            
        case .failure(_):
            print(response.result.error)
            break
            
        }
    }

and make sure that the parameters are of type

[String:Any]?

In case of Get

Alamofire.request("https://yourGetURL.com", method: .get, parameters: ["":""], encoding: URLEncoding.default, headers: nil).responseJSON { (response:DataResponse<Any>) in
        
        switch(response.result) {
        case .success(_):
            if let data = response.result.value{
                print(response.result.value)
            }
            break
            
        case .failure(_):
            print(response.result.error)
            break
            
        }
    }

Even works with

JSONEncoding.default 

For Headers

If you are passing headers, make sure their type should be [String:String]

Go through the Parameter Encoding Link https://github.com/Alamofire/Alamofire/blob/master/Documentation/Alamofire%204.0%20Migration%20Guide.md#parameter-encoding-protocol

Solution 3 - Alamofire

> Post method Alamofire 4.0 with Swift 3.0 and xCode 8.0

Alamofire.request(URL, method: .post, parameters: PARAMS)
                            .responseJSON { closureResponse in
                        if String(describing: closureResponse.result) == "SUCCESS"
                        { 
                           // Sucess code  
                        }
                        else
                        { 
                           // Failure Code 
                        }
                 }

Solution 4 - Alamofire

My solution is if you are using headers, its type must be [String:String].

Solution 5 - Alamofire

This error is up to parameters value. It has to be [String: String]

let url = URL(string: "http://yourURLhere")!
    
    let params: [String: String] = ["name": "oskarko", "email": "[email protected]", "sex": "male"]

    
    
    Alamofire.request(url, method: .post, parameters: params, encoding: URLEncoding.default, headers: nil).validate(statusCode: 200..<600).responseJSON() { response in
        
        switch response.result {
        case .success:
            
            var result = [String:String]()
            
            if let value = response.result.value {
                
                let json = JSON(value) 
                
            }
            
        case .failure(let error):
            print("RESPONSE ERROR: \(error)")
            
        }
        
    }

Solution 6 - Alamofire

I just resolved the same problem as you have. The problem is I have imported Alamofire in the header, so I just remove the Alamofire when call request. Like that:

> request(.POST, patientIdUrl, parameters: nil, headers: nil, encoding: > .JSON)

I hope it can help you.

Solution 7 - Alamofire

I ran into this same Extra argument 'method' in call error when my URL variable was out of scope.

In your case, please make sure both baseUrl and nextPatientIdUrl are in scope when they are being used Alamofire.request(patientIdUrl,..) method.

Hopefully this resolves your issue. Thanks You!

Solution 8 - Alamofire

func API()
{
    if Reachability.isConnectedToNetwork()
    {
        let headers = ["Vauthtoken":"Bearer \(apiToken)"]
        print(headers)
        //            let parameter = ["iLimit":"10","iOffset":"0","iThreadId":"1"]
        ApiUtillity.sharedInstance.showSVProgressHUD(text: "Loding...")
        Alamofire.request(ApiUtillity.sharedInstance.API(Join: "vehicle/CurrentVehicleLists"), method:.get, parameters:nil, headers: headers).responseJSON { response in
            switch response.result {
            case .success:
                print(response)
                ApiUtillity.sharedInstance.dismissSVProgressHUD()
                let dictVal = response.result.value
                let dictMain:NSDictionary = dictVal as! NSDictionary
                let statusCode = dictMain.value(forKey: "status") as! Int
                if(statusCode == 200)
                {

                }
                else if statusCode == 401
                {
                    
                }
                else
                {
                    
                }
            case .failure(let error):
                print(error)
                ApiUtillity.sharedInstance.dismissSVProgressHUD()
            }
        }
    } else
    {
        ApiUtillity.sharedInstance.dismissSVProgressHUD()
        ApiUtillity.sharedInstance.showErrorMessage(Title: "Internet Connection", SubTitle: "Internet connection Faild", ForNavigation: self.navigationController!)
    }
}

Solution 9 - Alamofire

For me this is working.

For GET Request

Alamofire.request("http://jsonplaceholder.typicode.com/todos/1/get").responseJSON { (response:DataResponse<Any>) in
        
        switch(response.result) {
        case .success(_):
            if response.result.value != nil{
                print(response.result.value!)
            }
            break
            
        case .failure(_):
            print(response.result.error)
            break
            
        }
        
    }

For POST

let parameters = NSDictionary(object: "nara", forKey: "simha" as NSCopying)
    
    Alamofire.request("http://jsonplaceholder.typicode.com/posts", method: HTTPMethod.post, parameters: parameters as? Parameters, encoding: JSONEncoding.default, headers: nil).responseJSON { (response:DataResponse<Any>) in

        
        switch(response.result) {
        case .success(_):
            if response.result.value != nil{
                print(response.result.value!)
            }
            break
            
        case .failure(_):
            print(response.result.error)
            break
            
        }
    }

Thanks @Rajan Maheswari.

Solution 10 - Alamofire

I fixed this issue with:

  1. Reorder parameters (url then method type).
  2. Change Encoding Enum to be "JSONEncoding.default" for example.

Note that: Alamofire methods signature change in Swift 3

Solution 11 - Alamofire

Two things that I found worth noting.

  1. Remove the first url label before its value. Use Alamofire.request("https://yourServiceURL.com", method: .post, instead of Alamofire.request(url: "https://yourServiceURL.com", method: .post,.
  2. Make sure the data type of the parameters is [String: String]. Declare it explicitly.

Solution 12 - Alamofire

I copy this code from Alamofire,create a URLRequest and used Alamofire.request(URLRequest) method, avoid this error

originalRequest = try URLRequest(url: url, method: method, headers: headers)
let encodedURLRequest = try encoding.encode(originalRequest!, with: parameters)

Solution 13 - Alamofire

I fixed this issue this way:

Just remove extra parameters, just parameters, encoding and headers, if these parameters are nil you can remove then and leave this way,

Alamofire.request(yourURLString, method: .post)

Solution 14 - Alamofire

If you have added Alamofire files locally then don't use "Alamofire" before request

let apipath = “your api URL”    
    request(apipath, method: .post, parameters: parameters, encoding: URLEncoding.default, headers: nil).responseJSON { response in switch(response.result) {
            case .success(_):
                do {
                    let JSON = try JSONSerialization.jsonObject(with: response.data! as Data, options:JSONSerialization.ReadingOptions(rawValue: 0))
    
                    guard let JSONDictionary: NSDictionary = JSON as? NSDictionary else {
                        print("Not a Dictionary")
                        return
                    }
    
                    print("Post Response : \(JSONDictionary)")
                }
                catch let JSONError as NSError {
                    print("\(JSONError)")
                }
                break
            case .failure(_):
                print("failure Http: \(String(describing: response.result.error?.localizedDescription))")
                break
            }
    }

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAgreenshView Question on Stackoverflow
Solution 1 - AlamofireAbdullah Md. ZubairView Answer on Stackoverflow
Solution 2 - AlamofireRajan MaheshwariView Answer on Stackoverflow
Solution 3 - AlamofireMohammad Kamran UsmaniView Answer on Stackoverflow
Solution 4 - AlamofirexevserView Answer on Stackoverflow
Solution 5 - AlamofireoskarkoView Answer on Stackoverflow
Solution 6 - AlamofireChi Minh TrinhView Answer on Stackoverflow
Solution 7 - AlamofireonlinebabaView Answer on Stackoverflow
Solution 8 - Alamofirejignesh kasundraView Answer on Stackoverflow
Solution 9 - AlamofireNarasimha NallamsettyView Answer on Stackoverflow
Solution 10 - AlamofireAhmed LotfyView Answer on Stackoverflow
Solution 11 - AlamofireJiang XiangView Answer on Stackoverflow
Solution 12 - AlamofireArthur LiuView Answer on Stackoverflow
Solution 13 - AlamofireVinicius CarvalhoView Answer on Stackoverflow
Solution 14 - AlamofirePriteshView Answer on Stackoverflow