To send a single JSON object to your server using Alamofire, you just have to convert your object to a serializable dictionary. For example:
extension Test {
func toDict() -> [String: String] {
return ["id": id ?? "", "name": name ?? ""]
}
}
And send it specifying the correct JSON encoding:
let parameters = test.toDict()
Alamofire.request(url, method: .post, parameters: parameters, encoding: JSONEncoding.default)
Sending an array of dictionaries instead of a dictionary is a little bit trickier. Code extracted from this answer:
let request = NSMutableURLRequest(URL: url)
request.HTTPMethod = "POST"
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
let values = tests.map { $0.toDict() }
request.HTTPBody = try! NSJSONSerialization.dataWithJSONObject(values, options: [])
Alamofire.request(request)
parameter: [ [ String: String] ]
toDict()
, that will return a Dictionary [String:String], with["id":id, "name":name]
, and then you create the array of theses.