SwiftyJSON throws incorrect data format error in Swift

206 views Asked by At

I have created a user authentication page. I am trying to Parse the JSON using SwiftyJSON (using Xcode 6.3). I have so far created something like this: After the user enters the id and password and on clicking the login button, the authentication process is done and the next controller in the segue is displayed. Consequently, the login page gives an id and a token for the user. I am using this id and token in the next page and parsing the json for the second page also(which contains a list of items)

//In viewDidLoad function
var request : NSMutableURLRequest = NSMutableURLRequest()
    request.URL = NSURL(string: URL) //I had defined the url already as a global variable
    request.HTTPMethod = "GET"

    request.addValue(String(self.id), forHTTPHeaderField: "id")
    request.addValue(self.token, forHTTPHeaderField: "token")

    //Parse JSON
    var parsedJSON = NSURLConnection.sendSynchronousRequest(request, returningResponse: nil, error: nil)
    if(parsedJSON != nil) {
        var JSONdata = JSON(data: parsedJSON!)
        println(JSONdata)
    }

But I am getting an error as "error: incorrect data format". I have tried pinging the url i specified with the id and token as parameters, using GET method from the terminal. I am getting the correct results, but Swift parsing doesn't happen here. What mistake am I doing?

0

There are 0 answers