1

I try to post mysql database with Alamofire and I succeed but my response is not seems json and I want to pass response values to model struct. I tried this code. Where is the wrong ? I would be very grateful if you can help.

 func register(registerModel:RegisterResponse,completion:@escaping (RegisterModel?)->() ){
    let parameters: [String: Any] =
    [
        "email": "\(registerModel.email)",
        "password": "\(registerModel.password)",
        "first_name": "\(registerModel.first_name)",
        "last_name": "\(registerModel.last_name)",
    ]
   
    AF.request(registerUrl,method: .post,parameters: parameters,encoding: URLEncoding.httpBody,headers: nil).responseJSON(){ response in
       //debugPrint(response)
        switch response.result{
        
        case .success(let data):
        
                if response.data != nil {
                    
                    print(data)
                    completion(data as? RegisterModel)
                    
                }
            
          
        case .failure(let err):
            print(err.localizedDescription)
        }
    }
    
}

output:

{
result = "account already exists";
tf = 0;
verificationCode = "<null>";
}
life4
  • 75
  • 10
  • 1
    Does this answer your question? [How to parse JSON response from Alamofire API in Swift?](https://stackoverflow.com/questions/26114831/how-to-parse-json-response-from-alamofire-api-in-swift) – Joakim Danielson May 21 '21 at 16:37
  • 1
    @JoakimDanielson No, I look at this question. I want to pass values my own class and this question is old there is one answer with swift 5 and it didn't fix my problem – life4 May 21 '21 at 16:39
  • 1
    But you are clearly doing it wrong since you can't cast `data` to your custom type. You need to decode it since it json. Take another look at the linked question – Joakim Danielson May 21 '21 at 16:42
  • 1
    The second answer might solve your problem https://stackoverflow.com/questions/31982513/how-to-send-a-post-request-with-body-in-swift – Mohamad Kaakati May 21 '21 at 16:51

2 Answers2

2

Assuming your RegisterResponse is Decodable, simply use responseDecodable:

AF.request(registerUrl, method: .post, parameters: parameters, encoding: URLEncoding.httpBody)
  .responseDecodable(of: RegisterResponse.self) { response in
    // Handle response.
  }
Jon Shier
  • 12,200
  • 3
  • 35
  • 37
1

Try this

AF.request(registerUrl, method: .post, parameters: parameters, encoding: JSONEncoding.default) .responseJSON { (response) in
    print(response)
}
Mohamad Kaakati
  • 392
  • 3
  • 11
  • I tried this method. output is same. Can you look at my edited question I added output. I couldn't use output variables. I want to pass this values a model struct – life4 May 21 '21 at 17:05
  • @life4 The output indicates that the POST method is working, the response you got from the server is that the `account already exists` try registering with another email if that is the issue, else if you want the response object to be mapped to a struct, you would want to create that struct and conform to Codable, try replacing `completion(data as? RegisterModel)` by following this explanation: https://www.hackingwithswift.com/articles/119/codable-cheat-sheet and then pass the new object to `completion` block without casting it. – Mohamad Kaakati May 21 '21 at 17:14