2

I'm connecting to my server on localhost to fetch some data. The data returned to this request is a JSON, on Postman the JSON is correctly shown, but when I'm playing with iOS, Alamofire returns me an error:

responseSerializationFailed(Alamofire.AFError.ResponseSerializationFailureReason.jsonSerializationFailed(Error Domain=NSCocoaErrorDomain Code=3840 "Garbage at end." UserInfo={NSDebugDescription=Garbage at end.}))


The JSON in question is:

{ "name": "TestName", "surname": "TestSurname" } 

The thing that I do not understand is that if I force my server to return the json in form of a string so something like

"{"name": "TestName after update","surname": "TestSurname"}" 

Alamofire does not complain and parses it correctly. How is that? I thought that specifying the parameter responseJSON it would have worked the other way around.

Alamofire.request("http://192.168.1.4:8080/user/abcdf").validate().responseJSON { response in switch response.result { case .success: // DO stuff case .failure(let error): print(error) } } 
4
  • I just checked the JSON by creating a json-server and it worked fine Commented May 31, 2017 at 22:22
  • But i added the JSON into an Object, like that : [ { name: "TestName", surname: "TestSurname" } ] Commented May 31, 2017 at 22:22
  • So probably it's the way the JSON is generated, will dig into it Commented May 31, 2017 at 22:41
  • That's definitely not a Alamofire bug. Check the answer from server. Commented May 31, 2017 at 23:39

2 Answers 2

5

This means your API response string is not a proper JSON. Ensure your response is valid JSON. In my case (below), JSON String had some HTML characters which broke the JSON.

enter image description here

If you are using Alamofire, change .responseJSON to .responseString and verify the response structure is valid JSON.

Note : if you are using Postman, you may not notice the extra unwanted character in JSON response. You need to change the response type from "Pretty" to "Raw" to observe this.

Sign up to request clarification or add additional context in comments.

1 Comment

It worked for me. My temporary debug code was unwanted character in JSON response.
0

I think you need to get the data so you should have it written like this I am not sure though

Alamofire.request("http://192.168.1.4:8080/user/abcdf",method:.get).responseJSON { response in if response.result.isSuccess { //do stuff } else { // do other stuff } } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.