Set timeout in Alamofire

Alamofire 5.1 includes a new way to modify the request with a closure in the initializer:

AF.request(url) { $0.timeoutInterval = 60 }
    .validate()
    .response { _ in // handle response here }

For new versions of AF, see https://stackoverflow.com/a/61192412/308315


For old versions, please try this:

    let request = NSMutableURLRequest(url: URL(string: "")!)
    request.httpMethod = "POST"
    request.setValue("application/json", forHTTPHeaderField: "Content-Type")
    request.timeoutInterval = 10 // 10 secs
    let values = ["key": "value"]
    request.httpBody = try! JSONSerialization.data(withJSONObject: values, options: [])
    Alamofire.request(request as! URLRequestConvertible).responseJSON {
        response in
        // do whatever you want here
    }