Conversion of Objective-c project to Swift (Server Call)

2

I have a problem that I explain, I think I found a way to call the methods that are on the server, however when I call a method that has parameters, I get an error, and when I call a method that has no parameters okay. Can someone explain to me why this is happening or what I am doing wrong?

Service call code

statusCode should be 200, but is 500
resposta do servidor ******** = Optional(<NSHTTPURLResponse: 0x7fc90a626350> { URL: https://*****.*****.com/****.slet request = NSMutableURLRequest(URL: NSURL(string: "https://*****.*****.com/****.svc/rest/ListRules")!)  
        request.HTTPMethod = "POST"

        let postString = "ID=10"    
        request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding)
        let task = NSURLSession.sharedSession().dataTaskWithRequest(request) { data, response, error in
            guard error == nil && data != nil else {                                                          // check for fundamental networking error
                print("error=\(error)")
                return
            }

            if let httpStatus = response as? NSHTTPURLResponse where httpStatus.statusCode != 200 {           // check for http errors
                print("statusCode should be 200, but is \(httpStatus.statusCode)")
                print("resposta do servidor ******** = \(response)")
            }

            let responseString = NSString(data: data!, encoding: NSUTF8StringEncoding)
            print("responseString ++++++++++++ = \(responseString)")
        }
        task.resume()

The error I get is as follows:

vc/rest/ListRules} { status code: 500, headers {
    "Cache-Control" = private;
    "Content-Length" = 1937;
    "Content-Type" = "application/xml; charset=utf-8";
    Date = "Wed, 27 Jan 2016 10:39:58 GMT";
    Server = "Microsoft-IIS/8.5";
    "X-AspNet-Version" = "4.0.30319";
    "X-Powered-By" = "ASP.NET";
} })
responseString ++++++++++++ = Optional(<Fault xmlns="http://schemas.microsoft.com/ws/2005/05/envelope/none"><Code>
<Value>Receiver</Value><Subcode><Value xmlns:a="http://schemas.microsoft.com/net/2005/12/windowscommunicationfoundation/dispatcher">a:InternalServiceFault</Value></Subcode></Code>
<Reason><Text xml:lang="en-US">The incoming message has an unexpected message format 'Raw'. The expected message formats for the operation are 'Xml', 'Json'. 
This can be because a WebContentTypeMapper has not been configured on the binding. See the documentation of WebContentTypeMapper for more details.</Text></Reason><Detail>
<ExceptionDetail xmlns="http://schemas.datacontract.org/2004/07/System.ServiceModel" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><HelpLink i:nil="true"/><InnerException i:nil="true"/>
<Message>The incoming message has an unexpected message format 'Raw'. The expected message formats for the operation are 'Xml', 'Json'. This can be because a WebContentTypeMapper has not been configured on the binding.
See the documentation of WebContentTypeMapper for more details.</Message><StackTrace>   at System.ServiceModel.Dispatcher.DemultiplexingDispatchMessageFormatter.DeserializeRequest(Message message, Object[] parameters)&#xD;
   at System.ServiceModel.Dispatcher.UriTemplateDispatchFormatter.DeserializeRequest(Message message, Object[] parameters)&#xD;
   at System.ServiceModel.Dispatcher.DispatchOperationRuntime.DeserializeInputs(MessageRpc&amp; rpc)&#xD;
   at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc&amp; rpc)&#xD;
   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc&amp; rpc)&#xD;
   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage31(MessageRpc&amp; rpc)&#xD;
   at System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)</StackTrace><Type>System.InvalidOperationException</Type></ExceptionDetail></Detail></Fault>)

Code that will solve the problem however I am having an error passing the function parameter

let request = NSMutableURLRequest(URL: NSURL(string: "https://*******.*******.com/******.***/***/METODO")!) request.HTTPMethod = "POST"
var postString:NSString = "ID=23"  //--> ERRO?

request.HTTPBody = postString.dataUsingEncoding (NSUTF8StringEncoding)

and the error is "Encountered unexpect charater 'I'" and this i is that of parameter ID, can someone help me?

    
asked by anonymous 28.01.2016 / 11:50

1 answer

1

I was able to solve my problem and the solution is as follows:

  let URL = NSURL(string: "https://****.*****.com/*****.svc/rest/GetByID")!
let mutableURLRequest = NSMutableURLRequest(URL: URL)
mutableURLRequest.HTTPMethod = "POST"

let parameters = ["ID": "23"]

do {
    mutableURLRequest.HTTPBody = try NSJSONSerialization.dataWithJSONObject(parameters, options: NSJSONWritingOptions())
} catch {
}

mutableURLRequest.setValue("application/json", forHTTPHeaderField: "Content-Type")

Alamofire.request(mutableURLRequest)
    .responseJSON{
        response in
        if let JSON = response.result.value{

            let dataSelected = [JSON.valueForKey("Price")!]         //Quando vem apenas um objecto
            print("RESULTADO \(dataSelected)")
        }
}
    
11.02.2016 / 19:22