Last active
June 1, 2018 10:33
-
-
Save farhan-syed/e5a3c74e4015ff44f702e2b48a18065d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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