Created
August 17, 2020 03:51
-
-
Save simrandotdev/fe72e015ddab0233ae88aaf09fcda268 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
| import UIKit | |
| func loadData() { | |
| do { | |
| guard let url = URL(string: "https://reqres.in/api/users") else { return } | |
| let body = [ | |
| "name": "Henry", | |
| "job": "Engineer" | |
| ] | |
| var request = URLRequest(url: url) | |
| request.httpMethod = "POST" | |
| request.httpBody = try JSONSerialization.data(withJSONObject: body, options: []) | |
| request.setValue("Application/json", forHTTPHeaderField: "Content-Type") | |
| URLSession.shared.dataTask(with: request) { (data, response, error) in | |
| if let error = error { | |
| print("Something went wrong with the request: \(error.localizedDescription)") | |
| return | |
| } | |
| guard let data = data else { | |
| print("No data found.") | |
| return | |
| } | |
| do { | |
| let decoder = JSONDecoder() | |
| let result = try decoder.decode(CreateUserResponse.self, from: data) | |
| print(result) | |
| } catch let error { | |
| print("Cannot convert the response to the required objects: \(error.localizedDescription)") | |
| } | |
| }.resume() | |
| } catch let error { | |
| print("Error : \(error.localizedDescription)") | |
| } | |
| } | |
| struct CreateUserResponse: Decodable { | |
| let name: String | |
| let job: String | |
| let id: String | |
| let createdAt: String | |
| } | |
| loadData() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment