0

I'm using Alamofire 3.0 and swift 2 and get a strange response from the following request:

func requestURLwithHeadersAndParams(URLstr:String, connectionMethod: Alamofire.Method, header: [String : String], body:[String : AnyObject], completion: (reponse: String, statusCode: Int, error: String) -> Void)  {        
///Singleton Alamofire connection manager
let alamofireManager = Alamofire.Manager.sharedInstance
alamofireManager.request(connectionMethod, URLstr, parameters: body, encoding: ParameterEncoding.JSON, headers: header).responseJSON (){
        response in

        print("\nSuccess: \(response.result.isSuccess)")

        switch response.result {
        case .Success(let value):
            print("Response Status code: \((response.response?.statusCode)!)\n")
            print("\(value)")
            completion(reponse: "\(value)" , statusCode: (response.response?.statusCode)! , error: "")

        case .Failure(let error):
            print("Error Code:\(error.code) - Description:\(error.localizedDescription)")
            completion(reponse: "Error" , statusCode: (response.response?.statusCode)!, error: error.localizedDescription)

        } } }

the value property contains:

"{\n    \"auth_token\" = \"qcW-mQmyX8ototieJu7WK\";\n    avatar =     {\n        standard =         {\n            url = \"<null>\";\n        };\n        url = \"<null>\";\n    };\n    birthdate = \"<null>\";\n    \"created_at\" = \"2015-10-28T07:02:20.445Z\";\n    email = \"elrope@abt.com\";\n    \"first_name\" = El;\n    id = 12;\n    \"last_name\" = Perro;\n    role = user;\n    \"shooter_type\" = \"\";\n    \"updated_at\" = \"2015-10-28T07:42:37.860Z\";\n}"

any idea how can I get rid of all the escape characters ('\' and '\n') since the JSON serialization using SwiftyJSON in the method calling this method can't recognize the string as a json string and fails when I do let json = JSON(reponse) with error:

Error Domain=SwiftyJSONErrorDomain Code=901 "Dictionary["auth_token"] failure, It is not an dictionary" UserInfo=0x6030002d7c00 {NSLocalizedDescription=Dictionary["auth_token"] failure, It is not an dictionary})

Arnab
  • 4,216
  • 2
  • 28
  • 50
Gutty1
  • 505
  • 2
  • 12
  • I'm curious. What type does the compiler say that value is? – Moriya Oct 28 '15 at 12:57
  • `reponse: "\(value)"` Why? Why do you do that? Why do you want it to be a String? If you want it to be a string, use the `responseString`, not the `responseJSON` (https://stackoverflow.com/questions/44095161/xcode-alamofire-get-string-response). You are calling `description` of what we can guess is a JSON with top level being a Dictionary already parsed. – Larme Apr 03 '18 at 12:34

2 Answers2

0

Your JSON is invalid.

Actually, your value string is nothing else than the .description output of an NSDictionary!

Demonstration in a Playground:

enter image description here

Needless to say that this isn't JSON... so of course it fails when you try to give this to SwiftyJSON.

So, either you were confused somewhere and you're using your dictionary's description instead of its actual content...

...or this error comes from the server.

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
0

I found your JSON invalid itself. e.g. avatar, standard are not strings, where are the quotes?

I belevie using , and : is better practice than using ; and =

I tested on SwiftyJSON and after above changes everything works fine. So maybe the issue is on your backend. Try to validate raw JSON using web browser.

zuziaka
  • 575
  • 3
  • 10
  • I changed in my call the Alamofire from responseJSON() to response() with completion: request, response, data, error instead of response. now the data is OK and I'm able to serialise it correctly with SwiftyJSON, but still is not clear why I received that response before from Alamofire responseJSON(). Could be a bug in Alamofire? – Gutty1 Oct 28 '15 at 15:04