3

I have read all the questions on this issue this and this. I have the following code

let fullURL = God.getFullURL(apiURL: self.apiUrl)
        if (getOrPost == God.POST) {
            Alamofire.request(fullURL, method: .POST, AnyObject: self.postData?, encoding:.JSONEncoding.default, headers: nil).responseJSON{ response in
                self.responseData = response.result.value
            }
        } else if (getOrPost == God.GET) {
            Alamofire.request(fullURL, method : .GET, Parameters: getData, encoding:.JSONEncoding.default, headers: nil).responseJSON{ response in
                self.responseData = response.result.value
            }
        }

My Swift and Xcode versions are

Apple Swift version 3.0.2 (swiftlang-800.0.63 clang-800.0.42.1)
Target: x86_64-apple-macosx10.9
Version 8.2.1 (8C1002)

My pod file is

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '10.0'
use_frameworks!

target 'Buseeta' do
        pod 'Alamofire', :git => 'https://github.com/Alamofire/Alamofire.git', :branch => 'master'
end

pod 'AlamofireObjectMapper', '~> 4.0'

pod 'SwiftyJSON'

I get Extra argument 'method' in call error on both Alamofire request lines.

Dont go mark this question as duplicate without carefully checking. I have fixed the code exactly as per the duplicate questions.

EDIT 1

I tried after removing the headers, same issue on .POST and .GET

let fullURL = God.getFullURL(apiURL: self.apiUrl)
        if (getOrPost == God.POST) {
            Alamofire.request(fullURL, method: .POST, AnyObject: self.postData?, encoding:.JSONEncoding.default).responseJSON{ response in
                self.responseData = response.result.value
            }
        } else if (getOrPost == God.GET) {
            Alamofire.request(fullURL, method : .GET, Parameters: getData?, encoding:.JSONEncoding.default).responseJSON{ response in
                self.responseData = response.result.value
            }
        }

EDIT 2

if (getOrPost == God.POST) {
            Alamofire.request(fullURL, method: .post, parameters: self.postData?, encoding:.JSONEncoding.default).responseJSON{ response in
                self.responseData = response.result.value
            }
        } else if (getOrPost == God.GET) {
            Alamofire.request(fullURL, method : .get, parameters: getData?, encoding:.JSONEncoding.default).responseJSON{ response in
                self.responseData = response.result.value
            }
        }

EDIT 3

I replaced with method : HTTPMethod.get, still no change. Same issue.

Community
  • 1
  • 1
Siddharth
  • 9,349
  • 16
  • 86
  • 148
  • You should update the method type. They should be lowercase such as .post and .get – ridvankucuk Feb 08 '17 at 13:16
  • This is a duplicate, your code is not similar at all, as you have argument like "AnyObject" – Lory Huz Feb 08 '17 at 13:17
  • tried both your recommendations friends. same issue.. no change – Siddharth Feb 08 '17 at 13:24
  • Try removing the leading `.` in `.JSONEncoding` in your EDIT 2. – redent84 Feb 08 '17 at 13:33
  • And the `?` in `getData?` and `self.postData?`, that doesn't make any sense either – redent84 Feb 08 '17 at 13:35
  • I removed the leading `.` and I guess it helped that parameter. But I am still stuck at `.post` and `.get` extra parameter issue. Do I need to import HTTPMethod or something ? – Siddharth Feb 08 '17 at 13:36
  • Try extracting all parameters to typed variables. If you've already fixed all problems described above, probably the `parameters` argument is not the expected type. Try `let parameters: [String: Any] = self.postData` (did you remember to remove the `?` here, right?) – redent84 Feb 08 '17 at 13:43

5 Answers5

1

You need to use another function to upload data:

func upload(
    _ data: Data,
    to url: URLConvertible,
    method: HTTPMethod = .post,
    headers: HTTPHeaders? = nil)
    -> UploadRequest

And parameters for GET request must be of type [String: Any]

Mo Abdul-Hameed
  • 6,030
  • 2
  • 23
  • 36
  • Tried `Alamofire.upload(postData: Data, to: fullURL, method: HTTPMethod.post, headers: nil).responseJSON { response in self.responseData = response.result.value }` I get an error `Ambigious reference to member upload(_:to:method:headers)` – Siddharth Feb 09 '17 at 05:00
0

The answer is in your link, here is the new definition of the method request:

let fullURL = God.getFullURL(apiURL: self.apiUrl)
        if (getOrPost == God.POST) {
            Alamofire.request(fullURL, method: .post, parameters: self.postData?, encoding:.JSONEncoding.default).responseJSON{ response in
                self.responseData = response.result.value
            }
        } else if (getOrPost == God.GET) {
            Alamofire.request(fullURL, method : .get, parameters: getData, encoding:.JSONEncoding.default).responseJSON{ response in
                self.responseData = response.result.value
            }
        }
Lory Huz
  • 1,557
  • 2
  • 15
  • 23
  • I have removed the headers.. exactly the same issue. No change at all. – Siddharth Feb 08 '17 at 13:09
  • again. exactly same issue. i made the change. Infact XCode shows `Parameters` in autocomplete – Siddharth Feb 08 '17 at 13:23
  • Strange, because the method definition is this: Alamofire.request(<#T##url: URLConvertible##URLConvertible#>, method: <#T##HTTPMethod#>, parameters: <#T##Parameters?#>, encoding: <#T##ParameterEncoding#>, headers: <#T##HTTPHeaders?#>) Try to clean your build folder – Lory Huz Feb 08 '17 at 13:32
  • yeah, did a clean up. same issue no change. – Siddharth Feb 08 '17 at 13:39
  • Still the damn `Extra argument method in call.` – Siddharth Feb 24 '17 at 07:34
0

What I tried was

var postData    : Parameters?
var getData    : Parameters?

in the init, I initalized the postData like this

self.postData       = try! JSONSerialization.data(withJSONObject: postData, options: JSONSerialization.WritingOptions.prettyPrinted)

and

 if (getOrPost == God.POST) {
            Alamofire.request(fullURL, method: HTTPMethod.post, parameters: postData as Parameters?, encoding: JSONEncoding.default, headers: nil).responseJSON{ response in
                self.responseData = response.result.value
            }
        } else if (getOrPost == God.GET) {
            Alamofire.request(fullURL, method: HTTPMethod.get, parameters: getData, encoding: JSONEncoding.default, headers: nil).responseJSON{ response in
                self.responseData = response.result.value
            }
        }

EDIT

This compiles without errors :). I tried the upload recommendation by @cosmicman66

if (getOrPost == God.POST) {
            Alamofire.upload(postData!, to: fullURL, headers:nil).responseJSON { response in
                self.responseData = response.result.value
            }
        } else if (getOrPost == God.GET) {
            Alamofire.request(fullURL, method: HTTPMethod.get, parameters: getData, encoding: JSONEncoding.default, headers: nil).responseJSON{ response in
                self.responseData = response.result.value
            }
        }

EDIT

This did not work. The request did not reach. I read somewhere that upload should not be used for POST.

Siddharth
  • 9,349
  • 16
  • 86
  • 148
  • 1
    Alamofire guys are not recommending `upload`. Unfortunately they are also not suggesting a solution. Seriously SUCKS.. – Siddharth Feb 24 '17 at 07:33
0

I got this error when I was passing an NSDictionary for parameters and not to mention the error message was misleading.

According to the documentation, the parameters of the request is of type [String: Any]. I got my issue fixed by doing so.

I’m on Xcode 8.3.1, Alamofire 4.4.0

This is the code that worked for me:

Alamofire.request(requestUrl, method: .post, parameters: requestParameters(), encoding: JSONEncoding.default, headers: nil)
            .responseJSON { response in

      print(response)
}

requestUrl is String, requestParameters() returns swift dictionary of type [String: Any]. And it is JSONEncoding.default, not .JSONEncoding.default

Refer this thread on GitHub for clarification:

https://github.com/Alamofire/Alamofire/issues/1508

Jithin
  • 913
  • 5
  • 6
-1

I am facing the same problem but by changing parameters type as [String : any] solve my problem

 let parameters : [String:any] = ["key":value]