Skip to content

Instantly share code, notes, and snippets.

@quangtqag
Last active March 11, 2020 03:52
Show Gist options
  • Save quangtqag/1139f090019401bb02477b21a2bf5ebb to your computer and use it in GitHub Desktop.
Save quangtqag/1139f090019401bb02477b21a2bf5ebb to your computer and use it in GitHub Desktop.
static func updateUser(id: Double, name: String, completionHandler: @escaping (_ userID: Double?, _ error: Error?) -> Void) {
let endpointUrl = URL(string: host + updateUserPath)!
var urlRequest = URLRequest(url: endpointUrl)
urlRequest.httpMethod = "PUT"
let userDict: [String: Any] = ["id": id, "name": name]
let userData = try! JSONSerialization.data(withJSONObject: userDict, options: [])
urlRequest.httpBody = userData
let task = URLSession.shared.dataTask(with: urlRequest) { (data, response, error) in
guard error == nil else {
print("error calling PUT on \(updateUserPath): \(error!)")
DispatchQueue.main.async {
completionHandler(nil, error)
}
return
}
let userData = try! JSONSerialization.jsonObject(with: data!, options: []) as! [String: Double]
let userID = userData["id"]
DispatchQueue.main.async {
completionHandler(userID, nil)
}
}
task.resume()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment