Search code examples
iosswiftalamofire

Alamofire parse JSON msg and status


I want to display the msg on UITextView using the status, but it display "Success: 200":

{ "status" : 500, "msg" : "\"Information is invalid\"" }

I'm using Swift4 in XCode10, here is my code:

import UIKit
import Alamofire
import SwiftyJSON

class VerifyAccount: UIViewController {

    let verify_url = "http://192.168.43.222:3000/mobile/account_verification"

    @IBOutlet weak var msgHandler: UITextView!

    func postData(url: String ,  parameters: [String : String]) {
        Alamofire.request(url, method: .post, parameters: parameters).responseJSON {
            response in
            if response.result.isSuccess {
                let postJSON : JSON = JSON(response.result.value!)
                print(postJSON)

                if let status = response.response?.statusCode {
                    switch(status) {
                    case 200:
                        self.msgHandler.text = ("Success: \(status)")
                    case 500:
                        self.msgHandler.text = ("Invalid: \(status)")
                    default:

                    }
                }
            } else {
                print("Error: \(String(describing: response.result.error))")
            }
        }
    }

    @IBAction func verifyBtn(_ sender: Any) {
        let compare : [String : String] = ["id" : id , "fname" : fname , "lname" : lname]

        postData(url: verify_url , parameters : compare)
    }
}

Solution

  • You need to change the way how you handling your response. I've updated your code, it should work.

    import UIKit
    import Alamofire
    import SwiftyJSON
    
    class VerifyAccount: UIViewController {
    
    let verify_url = "http://192.168.43.222:3000/mobile/account_verification"
    
    @IBOutlet weak var msgHandler: UITextView!
    
    func postData(url: String ,  parameters: [String : String]) {
        Alamofire.request(url, method: .post, parameters: parameters).responseJSON {
            response in
    
     switch response.result {
            case .success:
                print("server response: \(response.value!)")
    
    
                do {
                    if var json = try JSONSerialization.jsonObject(with: response.data!, options: [])  as? [String:Any] {
    
    
                        // Get value by key
                        var message = "";
                        if let msg = json["msg"] as? String
                        {
                            message = msg; // your message is here, you can do anything with it, whatever you want.
                        }
    
                        var status = 0;
                        if let st = json["status"] as? Int
                        {
                            status = st;
                        }
    
    
                    }
    
                } catch let error as NSError {
    
                }
    
    
    
                break
    
            case .failure(let error):
                self.delegate.APIOnError(requestCode: requestCode);
                print("server error: \(error.localizedDescription)")
    
            }
        }
      }