Print Message in Post Password Alamofire
I want to post data password and need feedback "message"
1. first i check data in Postman, select form-data and set key value like image below
Post -> https://yourApi/ios/v1/auth/password
response JSON in Postman
{
"status_code": 422,
"message": "All fields are required",
"errors": {
"old_password": "The old_password field is required.",
"password": "The password field is required.",
"verifypassword": "The verifypassword field is required."
}
}
follow or copy this code below :
import UIKit
import Alamofire
import SwiftyJSON
class PassViewController: UIViewController {
@IBOutlet weak var textInputOldPass: AnimatedTextInput!
@IBOutlet weak var textInputNewPass: AnimatedTextInput!
@IBOutlet weak var textInputRePass: AnimatedTextInput!
let urlPass = "https://yourApi/ios/v1/auth/password"
override func viewDidLoad() {
super.viewDidLoad()
self.title = "Password"
}
@IBAction func bSend(_ sender: Any) {
guard let oldPass = textInputOldPass.text, !(textInputOldPass.text?.isEmpty)! else { return }
guard let newPass = textInputNewPass.text, !(textInputNewPass.text?.isEmpty)! else { return }
guard let rePass = textInputRePass.text, !(textInputRePass.text?.isEmpty)! else { return }
let headers = ["Authorization": "\(UserDefaults.standard.object(forKey: "token") ?? "")"]
let parameters = [
"old_password": oldPass,
"password": newPass,
"verifypassword": rePass
]
Alamofire.request(urlPass, method: .post, parameters: parameters, encoding: JSONEncoding.default, headers: headers).responseJSON { response in
switch response.result {
case .success(let data):
print("dataList\(data)")
let dataJSON = JSON(data)
print("Test Message:\(dataJSON["message"])")
print("Test Status:\(dataJSON["status_code"])")
case .failure(let error):
print("dataList\(error)")
}
}
}
}
This result example from code above
Test Message: All fields are required
Test Status: 422
No comments: