15

Been working on this for a bit with no success. I have a function that goes to a UIButton solely to perform alamofire calls to my rails api which uses all JSON.

I'm using Swift 2, Alamofire 3, XCode 7 & Rails 4 for my api which is deployed to Heroku

I keep getting this error when I fire off the function :

alamofire.error Code=-6006 "JSON could not be serialized. Input data was nil or zero length.

Here is my code :

@IBAction func Save(sender: AnyObject) {

    let postsEndpoint: String = "https://APIURL"
    let parameters = [
        "users": [
            "name": "James McHarty",
            "avatar": "Some binary data",
            "post": [
                "title": "First Test Post",
                "body": "This is the first test post for the API",
                "liked": "8", //will make INT later
                "img": "more binary data"
            ]
        ]
    ]

    Alamofire.request(.POST, postsEndpoint, parameters: parameters, encoding: .JSON)
        .responseJSON { response in
            guard response.result.error == nil else {
            // got an error in getting the data, need to handle it
            print(response.result.error!)
            return
        }

    }

    print("func'd")

}
Victor Sigler
  • 23,243
  • 14
  • 88
  • 105
James H
  • 562
  • 2
  • 7
  • 20
  • 1
    Based on the error message, it looks like your server didn't send back any data. Did you try making the same request using `curl` or a similar HTTP tool? What JSON structure are you expecting? Why are you sure it's an iOS issue and not a Rails issue? – Aaron Brager Feb 13 '16 at 01:04
  • That was helpful input so I troubleshooted the server and went with a smaller request (user only) and my server accepted a POST request and now displays this when i perform a GET : [{"id":1,"name":"Don McHart","avatar":"some binary nonsense","posts":[]}]. This leads me to believe I'm doing something wrong in the parameters. – James H Feb 13 '16 at 01:51
  • You might want to close this question and open a new Rails question if the response body is not what you expect it to be. – Aaron Brager Feb 13 '16 at 05:14
  • This generally happens when you don't get valid response from server (ideally JSON)...In my case when server is restarted this happens. – Jayprakash Dubey Dec 06 '16 at 13:58
  • In my application, this error comes randomly. The server is running, never restarted. So we are unable to debug. What could be the reason in this case? – Pramod More May 22 '18 at 04:52
  • I am getting an empty response randomly. We can't figure out the problem yet. As of now, it looks like it is a server issue. Server is throwing the empty response sometimes but it is happing only at client side not our side that's why we can't generate this issue. Any help will be appreciated. – Hitesh Agarwal May 24 '19 at 07:12

3 Answers3

7

This is not Alamofire or swift error, The response returned by the server is not in the JSON format. you can print out response data and check what is wrong in this.

try this code to print out our server data to easily identifying to error and resolve this.

Alamofire.request("Your url").responseJSON(completionHandler: { (response) in
    switch response.result {
    case .success(let value):
        break

    case .failure(let error):
        print("\n\n===========Error===========")
        print("Error Code: \(error._code)")
        print("Error Messsage: \(error.localizedDescription)")
        if let data = response.data, let str = String(data: data, encoding: String.Encoding.utf8){
            print("Server Error: " + str)
        }
        debugPrint(error as Any)
        print("===========================\n\n")
    }

})
AshvinGudaliya
  • 3,234
  • 19
  • 37
3

The response returned by the server is not in the JSON format. You can use the tool to test the request first.

Print out of the error code is not a HTTP error code, because of the failure to resolve JSON

TBXark VFanx
  • 206
  • 2
  • 10
0

You need to check the mimeType it will be "text/plain" instead of "application/json". That's why JSONSerialization class not able to parse the data.

Yogendra Singh
  • 2,063
  • 25
  • 20