Swift 3.0, Alamofire 4.0 Extra argument 'method' in call

∥☆過路亽.° 提交于 2019-11-28 02:09:46
cosmicman66

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]

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
            }
        }

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.

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

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

 let parameters : [String:any] = ["key":value]
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!