Error404 Error404 - 23 days ago 16
Swift Question

Alamofire always enter on Failure status

I am trying to make a request with Alamofire and Swift 3.0 but it is always going to the failure status.

This is the code I have right now:

let str = "http://url/{\"user\":\"1\",\"pass\":\"1\"}"

let encodedUrl = str.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)

Alamofire.request(encodedUrl!, method: .get, parameters: nil, encoding: JSONEncoding.default, headers: nil).responseJSON { response in

switch(response.result) {
case .success(_):

print("Success")

break

case .failure(_):
print("Failure")
break

}
}


If I use the
encodedUrl
on
Postman
application I get the response well. It gives to me the following JSON:

[
{
"user": "1",
"name": "peter"
},
{
"user": "4",
"name": "andrew"
}]


What am I missing?

Thanks in advance!

Answer

Finally, it seems that I was retrieving an extra String out of the JSON because I used an echo on my API.

I could not see it because I had on Postman the option to show the response as JSON so the String was not being shown. I have changed it to HTML and then I was able to see the String.

Removing the extra String let me enter on the success status of Alamofire.