Search code examples
swift2alamofireform-data

Using FORM DATA with Alamofire


I am using alamofire for some time now, but I have never used a form data Post. Now I am stuck. I have 2 params (email, password) and don't know how POST them to server. Can anyone give me an example please?


Solution

  • So my solution is.... you have to specify the Parameter Encoding in Alamofire. So the code will look like this.

    Swift 2.0

    func registerNewUserFormData(completionHandler: (Bool, String?) -> ()){
    
            // build parameters
            let parameters = ["email": "test@test.cz", "password": "123456"]
    
            // build request
            Alamofire.request(.POST, urlDomain + "register", parameters: parameters, encoding: .URL).responseJSON { response in
    
                switch response.result {
                case .Success:
                    print("Validation Successful")
                    if let JSON = response.result.value {
                        print(JSON)
                    }
    
                case .Failure(let error):
                    print(error)
    
                }
            }
        }