class func postLoginRequest(url: String, parameters: Parameters?, success: @escaping (Any) -> Void, failure: @escaping (NetworkError) -> Void) {
if Network.isAvailable {
let param : [String : Any] = [
"username" : "x",
"password" : "x"
]
print (type(of: param))
print(param)
print(type(of: parameters))
print(parameters)
let manager = Alamofire.Session.default
manager.session.configuration.timeoutIntervalForRequest = Constants.NetworkError.timeOutInterval
manager.request(url, method: .post, parameters: param, encoding: JSONEncoding.default ).validate(statusCode: 200..<600).responseJSON { (response) -> Void in ...
My data output like given in bellow;
Dictionary<String, Any>
["username": "x", "password": "x"]
Optional<Dictionary<String, Any>>
Optional(["username": Optional(<TextFieldEffects.HoshiTextField: 0x11c054600; baseClass = UITextField; frame = (41 10; 286 40); text = 'x'; opaque = NO; autoresize = RM BM; gestureRecognizers = <NSArray: 0x2821b45d0>; layer = <CALayer: 0x282f855c0>>), "password": Optional(<TextFieldEffects.HoshiTextField: 0x11c043600; baseClass = UITextField; frame = (41 10; 286 40); text = 'x'; opaque = NO; autoresize = RM BM; gestureRecognizers = <NSArray: 0x2821afe40>; layer = <CALayer: 0x282f849c0>>)])
When ı use param as a parameters no problem. I can get username and password in backend (DJango) for jwt auth. But ı cant when ı use parameters as parameters value. How can do this convertion to send json body data.
CodePudding user response:
You need to safely unwrap to convert anything from Optional to Type
let myDict: [String : Any]? = ["john" : "Appleseed"]
// 1. Using guard statements
guard let unwraped: [String : Any] = myDict else {
// The value is nil
return
}
// 2. Using if statements
if let unwraped: [String : Any] = myDict {
// unwraped value
}
else {
// The value is nil
}
// 3. Using force unwrap (be careful, this will crash your app if myDict is nil)
let unwraped = myDict!
CodePudding user response:
My mistake is sending the UITextField as value. I missed it. First I fixed this. After that ı use URLEncoding.httpBody for encoding data.
if Network.isAvailable {
let manager = Alamofire.Session.default
manager.session.configuration.timeoutIntervalForRequest = Constants.NetworkError.timeOutInterval
manager.request(url, method: .post, parameters: parameters, encoding: URLEncoding.httpBody ).validate(statusCode: 200..<600).responseJSON { (response) -> Void in
Thanks.