14

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?

Sam
  • 159
  • 1
  • 2
  • 12

2 Answers2

39

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)
        }
    }
Arafin Russell
  • 1,487
  • 1
  • 18
  • 37
  • you didn't get what my point is....i am saying that i can get different strings as a response too ...like wrong_password,userlocked,etc etc and if password and username matches then i will get json...how can i get the string response? i my case i want to print wrong_password or userlocked or any string i got from php – Sam May 21 '17 at 10:08
  • what **data** do you get in **print(data)** when the credentials are wrong in the log ? – Arafin Russell May 21 '17 at 10:30
  • thats the point...in log it always get to .failure it never gets to .success because the data i get on wrong credentials is not json..its simple a string/..."wrong_password" – Sam May 21 '17 at 10:49
  • So as I can see you want **JSON** & **String** both return when the response is successful, so use both. I have updated my answer. – Arafin Russell May 21 '17 at 10:53
  • thank you..can you write all the alamofire code? so can i could understand well? that would be pretty helpfull – Sam May 21 '17 at 11:02
  • Try this book to learn everything about **Alamofire** with **Swift**, this is the best book so far I have read, [iOS Apps with REST](https://leanpub.com/iosappswithrest) , also do not forget to accept the answer if that helps you. Thanks – Arafin Russell May 21 '17 at 18:22
  • This response is outdated, please update if possible. – Pedro Paulo Amorim Jul 13 '20 at 16:12
  • Updated @PedroPauloAmorim – Arafin Russell Jul 29 '20 at 04:07
4

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

Firda Sahidi
  • 1,227
  • 1
  • 11
  • 22