Alamofire Swift 3.0电话中的额外参数

Agr*_*nsh 53 alamofire swift3

我已将我的项目迁移到Swift 3(并在Podfile中将Alamofire更新为最新的Swift 3版本pod 'Alamofire', '~> 4.0').

我现在在每个Alamofire.request上得到一个"额外参数调用"错误.例如:

let patientIdUrl = baseUrl + nextPatientIdUrl
Alamofire.request(.POST, patientIdUrl, parameters: nil, headers: nil, encoding: .JSON)
Run Code Online (Sandbox Code Playgroud)

谁能告诉我为什么?

Abd*_*air 78

根据Alamofire版本4.0.0的文档,使用HTTP方法的URL请求如下:

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)
Run Code Online (Sandbox Code Playgroud)

所以你的网址请求将是:

Alamofire.request(patientIdUrl, method: .post, parameters: nil, encoding: JSONEncoding.default, headers: nil)
Run Code Online (Sandbox Code Playgroud)

并且样本请求将是:

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
}
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助!

  • 它有效吗?在使用它时,我仍然在调用中得到额外参数的错误`Alamofire.request(url,method:.post,parameters:nil,headers:nil,encoding:JSONEncoding.default) (8认同)

Raj*_*ari 69

这个对我有用.
无需删除编码参数

Swift 3.x/4.x.

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)
    }
}
Run Code Online (Sandbox Code Playgroud)

并确保参数是类型

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

        }
    }
Run Code Online (Sandbox Code Playgroud)

如果是Get

[String:Any]?
Run Code Online (Sandbox Code Playgroud)

甚至可以使用

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

        }
    }
Run Code Online (Sandbox Code Playgroud)

对于标题

如果您要传递标题,请确保它们的类型 Result

浏览Alamofire.request https://github.com/Alamofire/Alamofire/blob/master/Documentation/Alamofire%204.0%20Migration%20Guide.md#parameter-encoding-protocol

  • @ Error404编码取决于服务器端的API类型.如果它接受后参数不是原始形式,那么你必须使用`URLEncoding.default`.如果它接受post参数作为原始形式,那么你必须选择`JSONEncoding.default`在`GET`你不需要它们中的任何一个.如果它接受paramters作为表单数据的多部分形式,那么你必须使用alamofire的`multipart`功能 (2认同)

小智 5

使用Swift 3.0和xCode 8.0发布方法Alamofire 4.0

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