Search code examples
iosswiftsoapalamofire

alamofire send request via soap


i use alamofire . before convert code to swift3 , everything was ok but after convert to swift3 when call my method ... data be empty .

getRequest return a NSMutableURLRequest :

private func getRequest(_ soapAction: String, urlService: URL, methodName: String, params: NSArray?) -> NSMutableURLRequest {
    var newSoapMessage = ""

    if let _ = params {
        newSoapMessage = getSoapMessage(methodName, params: params!)
    } else {
        newSoapMessage = getSoapMessage(methodName)
    }

    request = NSMutableURLRequest(url: urlService)
    lengthSoapMessage = String(newSoapMessage.characters.count)

    request.addValue("text/xml; charset=utf-8", forHTTPHeaderField: "Content-Type")
    request.addValue(lengthSoapMessage, forHTTPHeaderField: "Content-Length")
    request.addValue("Basic \(accountBase64)", forHTTPHeaderField: "Authorization")
    request.addValue(soapAction, forHTTPHeaderField: "SoapAction")
    request.httpMethod = "POST"
    request.httpBody = newSoapMessage.data(using: String.Encoding.utf8)

    return request
}

now :

var request = getRequest(soapAction,urlService: Common.urlShatelService! as URL, methodName: methodName, params: params)



    Alamofire.request(request as! URLRequestConvertible).responseData { response in
        switch response.result {
        case .success(let value):
            let result = Result.success(value)
            callBack(result)
            break
        case .failure:
            let result = Result<Data>.failure
            callBack(result)
            print("exception: \(response.result.error)")
            break
        }
    }

before convert , alamofire accept a request but after convert i must cast to URLRequestConvertible !!!!

now . when call alamofire return this exception :

2016-09-27 17:17:57.267619[3454:1060477] Could not cast value
 of type 'NSMutableURLRequest' (0x101e445b8) to 
'Alamofire.URLRequestConvertible' (0x101e400b0).

Solution

  • I had the same issue. Try changing the type NSMutableURLRequest for URLRequest.

    request = URLRequest(url: urlService)