0
votes

I got a response from my Alamofire post request. I want to get the Status code from the response. Here in the code:

case .success(let upload, _, _):
    upload.responseJSON { response in
        print(response.result.value!)
    }

Here is the response looks like:

Response image

2

2 Answers

0
votes

The value property of the response's result is a dictionary. You can extract status like this:

let json = response.result.value as? [String: Any]
if let status = json?["status"] as? Int {
    print(status)
}
0
votes

Use responseDecodable in Alamofire 5 with Codable Structure

.responseDecodable { (response: AFDataResponse<ProfileModel>) in
        switch response.result {
        case .success(let profileModel):
            switch profileModel.status {
            case 200:
                print("success")
            case 101:
                print("sessionExpire")
            default:
                print("default")
            }
        case .failure(let error):
            print("failure")
        }
    }

ProfileModel

struct ProfileModel: Codable {
    let status: Int
    let type: String
    let data: ProfileDataModel
}

struct ProfileDataModel: Codable {
    let ImagePath: String
    let ThumbImagePath: String
}