Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xcode : Alamofire get String response

I am new in IOS development and currently learning networking with Alamofire

i am trying to make a login ... whenever the credentials are correct the .php file returns a json and i am able to get that json from Alamofire through the following code:

    Alamofire.request(loginUrl, method: .post, parameters: parameters).responseJSON { (response:DataResponse<Any>) in
        print("String:\(response.result.value)")
        switch(response.result) {
        case .success(_):
            if let data = response.result.value{
                print(self.loginUrl)
                print(data)
            }

        case .failure(_):

            print(self.loginUrl)
            print("failed")
            print("Error message:\(response.result.error)")
            break

        }
    }

now...when ever the credentials are wrong, the .php does't give the json..instead it return a string ..for example "wrong_password" or "userLocked" etc etc... how can i get the String response through Alamofire?

like image 352
Sam Avatar asked May 21 '17 09:05

Sam


2 Answers

If you want JSON response use .responseJSON , if you want String response use .responseString. If you want both use both. Hope this help.

Alamofire.request(loginUrl, method: .post, parameters: parameters)
     .responseJSON { response in
       print("JSON:\(response.result.value)")
       switch(response.result) {
       case .success(_):
          if let data = response.result.value{
             print(data)
           }
            
        case .failure(_):
            
            print("Error message:\(response.result.error)")
            break
            
        }
    }
     .responseString { response in
       print("String:\(response.result.value)")
       switch(response.result) {
       case .success(_):
          if let data = response.result.value{
             print(data)
            }
                
       case .failure(_):
           print("Error message:\(response.result.error)")
           break     
        }
    }

UPDATED: Swift 5, Alamofire 5

    AF.request(urlString, method: .post, parameters: parameters)
        .responseJSON { response in
            print("response: \(response)")
            switch response.result {
            case .success(let value):
                print("value**: \(value)")
                
            case .failure(let error):
                print(error)
            }
    }
    .responseString { response in
        print("response: \(response)")
        switch response.result {
        case .success(let value):
            print("value**: \(value)")
            
        case .failure(let error):
            print(error)
        }
    }
like image 120
Arafin Russell Avatar answered Sep 24 '22 13:09

Arafin Russell


I resolve this by:

print(response.request)  // original URL request
print(response.response) // URL response
print(response.data)     // server data
print(response.result)   // result of response serialization

source: https://github.com/Alamofire/Alamofire/issues/818

like image 23
Firda Sahidi Avatar answered Sep 22 '22 13:09

Firda Sahidi