Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 467
Pan Mluvčí Avatar asked Nov 23 '15 10:11

Pan Mluvčí


People also ask

How do you use multipart form data?

Multipart form data: The ENCTYPE attribute of <form> tag specifies the method of encoding for the form data. It is one of the two ways of encoding the HTML form. It is specifically used when file uploading is required in HTML form. It sends the form data to server in multiple parts because of large size of file.

What is the advantage of Alamofire?

Advantages of Using AlamofireUsing Alamofire will give a cleaner project. API call interactions (POST/GET/PUT/etc.) will be easier and more understable. Alamofire simplifies a number of common networking tasks that makes development faster and easier.

Can you use Alamofire Objective C?

The alamofire methods return a Request object which does not inherit from NSObject and is, therefore, not visible in Objective-c.


2 Answers

Swift 5

let url = "http://testurl.com"

let parameters = [
    "email": "[email protected]",
    "password": "55555"
]


AF.request(url, method: .post, parameters: parameters, encoding: URLEncoding.default).responseJSON { response in
    switch response.result {
    case .success:
        if let value = response.result.value {
            print(value)
        }
    case .failure(let error):
        print(error)
    }
}
like image 94
Mohammad Sadiq Avatar answered Oct 30 '22 02:10

Mohammad Sadiq


And here is a sample code for Alamofire 4.0 in Swift 3.0

    let url = "http://testurl.com"

    let parameters = [
        "email": "[email protected]",
        "password": "55555"
    ]


    Alamofire.request(url, method: .post, parameters: parameters, encoding: URLEncoding.default).responseJSON { response in
        switch response.result {
        case .success:
            if let value = response.result.value {
                print(value)
            }
        case .failure(let error):
            print(error)
        }
    }
like image 20
Ahmad Ismail Avatar answered Oct 30 '22 03:10

Ahmad Ismail