import Foundation class HttpRequest { func sendGet(urlString: String, cb: @escaping([String:String])->()) { let session = URLSession.shared let url = URL(string: urlString)! let task = session.dataTask(with: url) { data, response, error in if error != nil || data == nil { print("Client error!") return } guard let response = response as? HTTPURLResponse else { print("Server error!") return } guard (200...399).contains(response.statusCode) else{ print(response.statusCode) return } guard let mime = response.mimeType, mime == "application/json" else { print("Wrong MIME type!") return } do { let json = try JSONSerialization.jsonObject(with: data!, options: []) as? [String: String] cb(json!) } catch { print("JSON error: \(error.localizedDescription)") } } task.resume() } func sendPost(urlString: String, header:[String:String], cb: @escaping([String:Any])->()) { let session = URLSession.shared let url = URL(string: urlString)! var request = URLRequest(url: url) for (key,value) in header { request.setValue(value, forHTTPHeaderField: key) } request.httpMethod = "POST" let task = session.dataTask(with: request) { data, response, error in if error != nil || data == nil { print("Client error!") return } guard let response = response as? HTTPURLResponse else { print("Server error!") return } guard (200...399).contains(response.statusCode) else{ print(response.statusCode) return } guard let mime = response.mimeType, mime == "application/json" else { print("Wrong MIME type!") return } do { let json = try JSONSerialization.jsonObject(with: data!, options: []) as? [String: Any] cb(json!) } catch { print("JSON error: \(error.localizedDescription)") } } task.resume() } func sendPost(urlString: String, header:[String:String], jsonData:Data, cb: @escaping([String:Any])->()) { let session = URLSession.shared let url = URL(string: urlString)! var request = URLRequest(url: url) for (key,value) in header { request.setValue(value, forHTTPHeaderField: key) } request.httpMethod = "POST" request.httpBody = jsonData let task = session.dataTask(with: request) { data, response, error in if error != nil || data == nil { print("Client error!") return } guard let response = response as? HTTPURLResponse else { print("Server error!") return } guard (200...399).contains(response.statusCode) else{ print(response.statusCode) return } guard let mime = response.mimeType, mime == "application/json" else { print("Wrong MIME type!") return } do { let json = try JSONSerialization.jsonObject(with: data!, options: []) as? [String: Any] cb(json!) } catch { print("JSON error: \(error.localizedDescription)") } } task.resume() } }
hr.sendGet(urlString:"https://example.com",cb:{ result in let user = result["user"]! let password = result["password"]! var header_dict = [String:String]() header_dict["Content-Type"] = "application/json" let formData:[String:String] = ["user":user,"password":password] let jsonData = try? JSONSerialization.data(withJSONObject: formData) hr.sendPost(urlString: "https://example.com/post/api", header:header_dict,jsonData: jsonData!,cb:{ result in print(result) }) })