December 2018
Intermediate to advanced
414 pages
10h 19m
English
Now we are ready to add the first few methods to our library. The first thing we want to do is add the GET HTTP method. To this aim add the following code to the Sources/swURL/swURL.swift file that SPM created for you:
public func get<T: Decodable>(url: URL, completion: @escaping(_ data: T?, _ error: Error?) -> Void) -> Void { jsonRequest(urlRequest: URLRequest(url: url), completion: completion)}
Let's also add a simple implementation to the POST request:
public func post<T: Encodable, U: Decodable>(url: URL, body: T, completion: @escaping (_ data: U?, _ error: Error?) -> Void) -> Void { var request = URLRequest(url: url) request.httpMethod = "POST" request.httpBody = try? JSONEncoder().encode(body) jsonRequest(urlRequest: ...Read now
Unlock full access