Skip to content

Instantly share code, notes, and snippets.

@farhan-syed
Last active June 1, 2018 10:33
Show Gist options
  • Save farhan-syed/e5a3c74e4015ff44f702e2b48a18065d to your computer and use it in GitHub Desktop.
Save farhan-syed/e5a3c74e4015ff44f702e2b48a18065d to your computer and use it in GitHub Desktop.
let postsURLEndPoint: String = "https://jsonplaceholder.typicode.com/posts"
let newPost: [String: Any] = ["userId" : 12345, "title": "This is a POST request", "Body": "This reqeust is sent with Alamofire"]
Alamofire.request(postsURLEndPoint, method: .post, parameters: newPost,
encoding: JSONEncoding.default)
.responseJSON { response in
guard response.result.error == nil else {
// got an error in getting the data, need to handle it
print("error")
print(response.result.error!)
return
}
// unwrap JSON
guard let json = response.result.value as? [String: Any] else {
print("No JSON")
// Could not get JSON
return
}
// use json
guard let postTitle = json["title"] as? String else {
// Could not get title from json
return
}
print("Post title: " + postTitle)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment