I've slightly edited SwiftDeveloper's answer, because it wasn't working for me. I added Alamofire validation as well.
let body: NSMutableDictionary? = [
"name": "\(nameLabel.text!)",
"phone": "\(phoneLabel.text!))"]
let url = NSURL(string: "http://server.com" as String)
var request = URLRequest(url: url! as URL)
request.httpMethod = "POST"
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
let data = try! JSONSerialization.data(withJSONObject: body!, options: JSONSerialization.WritingOptions.prettyPrinted)
let json = NSString(data: data, encoding: String.Encoding.utf8.rawValue)
if let json = json {
print(json)
}
request.httpBody = json!.data(using: String.Encoding.utf8.rawValue)
let alamoRequest = Alamofire.request(request as URLRequestConvertible)
alamoRequest.validate(statusCode: 200..<300)
alamoRequest.responseString { response in
switch response.result {
case .success:
...
case .failure(let error):
...
}
}