0
votes

I am using Alamofire in my App. This is my Alamofire request code

let params: [String:AnyObject] = ["email": self.signin_Email.text!, "password": self.signin_Password.text!]

               Alamofire.request(.GET, "http://DomainName/api/App/Sign_Up", parameters: params, encoding:.JSON)
                    .responseJSON {  response  in
                     debugPrint(response)
                }

when i put debugPrint(reponse) what i got is this

[Request]: { URL: http://Domain/api/App/Sign_Up } [Response]: nil [Data]: 0 bytes [Result]: FAILURE: Error Domain=NSURLErrorDomain Code=-1017 "cannot parse response" UserInfo={NSUnderlyingError=0x7ffe0840e700 {Error Domain=kCFErrorDomainCFNetwork Code=-1017 "(null)" UserInfo={_kCFStreamErrorCodeKey=-1, _kCFStreamErrorDomainKey=4}}, NSErrorFailingURLStringKey=http://Domain/api/App/Sign_Up, NSErrorFailingURLKey=http://Domain/api/App/Sign_Up, _kCFStreamErrorDomainKey=4, _kCFStreamErrorCodeKey=-1, NSLocalizedDescription=cannot parse response}

Here i am always getting the response status as Failure. I am not able to figure out what's going on. (using mvc4 as backend). This is the Api method that accepts the above given request and returns a JSON Data

 [System.Web.Http.HttpGet]
    public JsonResult Sign_Up(string email,string password)
    {
        email = email;
        password = password;
        System.Web.Mvc.JsonResult usertoreturn = new System.Web.Mvc.JsonResult();
        SignUpViewModel signupviewmodel = new SignUpViewModel();

                usermodeltocheck.SetPassword(password);
                usermodeltocheck.MembershipDate = DateTime.Now;
                usermodeltocheck.IsMember = true;
                usermodeltocheck.PublicKey = Guid.NewGuid().ToString("N");
                usermodeltocheck.MembershipStatus = true;
                usertoreturn.Data = Helper.UpdateUser(usermodeltocheck);
            }

       usertoreturn.JsonRequestBehavior = System.Web.Mvc.JsonRequestBehavior.AllowGet;

        return usertoreturn;
    }

UPDATE #1 I have created a new method named test that accepts a parameter.The method just returns the parameter value .I tried the sample code available in Github and its working. I am able to get proper response if I am avoiding parameters argument in Alamofire request Method. like

Alamofire.request(.GET, "http://DomainName/api/App/Test", encoding: .JSON).responseJSON{
                    response in
                debugPrint(response)

                }

here I am getting a SUCCESS response. I have updated my code like this

Alamofire.request(.GET, "http://DomianName/api/App/Test?test=testing", encoding: .JSON).responseJSON{
                    response in
                debugPrint(response)

                }

here also I am getting SUCCESS response. The Error occurs when I pass parameter value to the argument parameters parameters: ["test":"testing"]. also I set my parameters like this

let params = ["test":"testing"]
 Alamofire.request(.GET, "http://DomianName/api/App/Test", parameters : params ,encoding: .JSON).responseJSON{
                        response in
                    debugPrint(response)

                    }

in this way also i am getting my response to FAILURE

3

3 Answers

5
votes

May be its not an answer you are looking for but for me removing a parameter from Alamofire request method did the trick. Here is the change:

let params : [String:AnyObject] = ["email":self.signin_Email.text!,"password":self.signin_Password.text!]

let request =  Alamofire.request(.GET, "http://DomianName/api/App/Sign_Up", parameters: params).responseJSON{
    response in

    switch response.result{

    case .Success(let data) :

        let json = JSON(data)
        print(json)

    case .Failure(let error):
        print("Error : \(error)" )

    }
}

I have removed encoding:.JSON from my Alamofire request method parameter list and that's it...

0
votes

Try to print out all the data in response using the following:

let URLString = "http://DomainName/api/App/Sign_Up"
Alamofire.request(.GET, URLString, parameters: params, encoding:.JSON)
    .responseJSON { response in
        debugPrint(response)
    }

Once you print it out, if you could update your question, we could help further. I'll update my answer accordingly afterwards. 👍🏼


Update #1

Okay, so the NSURLErrorDomain Code=-1017 points out that your server is most likely misbehaving. Are you able to successfully use cURL, Postman, Paw or some other HTTP client to hit the service? Once you get one of those working, you should use debugPrint on the `request object to compare.

let URLString = "http://DomainName/api/App/Sign_Up"
let request = Alamofire.request(.GET, URLString, parameters: params, encoding:.JSON)
    .responseJSON { response in
        debugPrint(response)
    }

debugPrint(request)

This will show you the cURL command for the request.

0
votes

I know this is kind of old but I stumbled upon this looking for something else. From what I have seen, I tend to get errors in this situation any time params are passed as JSON encoded with a .GET instead of a .POST

Changing the server to take a post for the URI makes everything flow correctly, and I guess in theory that is correct behavior, since if you aren't passing the values in the URL, you are technically posting the JSON to the endpoint.