Alamofire with custom parameter encoding for swift application

為{幸葍}努か 提交于 2019-12-21 20:05:11

问题


I have to call some methods from the soap web service in my swift application, so I think I should use custom parameter encoding, but when I create closure for this encoding it seems never to be called. Am I doing something wrong?

Here is my code:

    let custom: (URLRequestConvertible, [String: AnyObject]?) -> (NSURLRequest, NSError?) = {
        (URLRequest, parameters) in
        let mutableURLRequest = URLRequest.URLRequest.mutableCopy() as NSMutableURLRequest
        mutableURLRequest.setValue("text/xml; charset=utf-8", forHTTPHeaderField: "Content-Type")
        mutableURLRequest.HTTPBody = body
        return (mutableURLRequest, nil)
    }

    Alamofire.request(.POST, WebServiceURLString, parameters: nil, encoding: .Custom(custom)).responseString { (request, response, data, error) -> Void in
        println(data)
    }

回答1:


The custom closure is not executed by design, because there are no parameters to encode. He're a code excerpt taken from Alamofire.swift source file:

if parameters == nil {
    return (URLRequest.URLRequest, nil)
}

As you can see, you can bybass this condition by passing an empty dictionary:

Alamofire.request(.POST, WebServiceURLString, parameters: Dictionary(), encoding: .Custom(custom))

The custom closure will now be executed.



来源:https://stackoverflow.com/questions/26684688/alamofire-with-custom-parameter-encoding-for-swift-application

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