GET
ViewController.swift
import Cocoa class ViewController: NSViewController { var sites:[SiteHost] = [SiteHost]() struct SiteHost:Decodable{ var id:String="" var name:String="" var url:String="" var description:String="" var logo_path:String="" } func loadList(str:String="") { let urlString = "https://programmerdesign.com/go_through/site-host/reload?str="+str+"&access-token="+UserDefaults.standard.string(forKey: "token")! if let url = URL(string:urlString) { let task = URLSession.shared.dataTask(with: url, completionHandler:{ data, response, error in // Check the response let decoder = JSONDecoder() do{ let site_hosts=try decoder.decode([SiteHost].self,from:data!) self.sites = site_hosts DispatchQueue.main.async{ // self.siteView.reloadData() // self.siteView.beginUpdates() // self.siteView.endUpdates() } }catch{ print(error.localizedDescription); } }) task.resume() } } }
POST
ViewController.swift
import Cocoa class ViewController: NSViewController { var site:SiteHost = SiteHost() func save(id:String,siteDescription:String,siteName:String,siteUrl:String,imagePath:String) { let urlString = "https://programmerdesign.com/go_through/site-host/update?id="+id+"&access-token="+UserDefaults.standard.string(forKey: "token")! if let url = URL(string:urlString) { var request = URLRequest(url: url) request.httpMethod = "POST" request.setValue("application/json", forHTTPHeaderField: "Content-Type") request.setValue("Powered by Swift!", forHTTPHeaderField: "X-Powered-By") let json = [ "name": siteName, "url": siteUrl, "logo_path": imagePath, "description": siteDescription, ] let jsonData = try! JSONSerialization.data(withJSONObject: json, options: []) let task = URLSession.shared.uploadTask(with: request ,from:jsonData, completionHandler:{ data, response, error in // Check the response let decoder = JSONDecoder() do{ let shost = try decoder.decode(SiteHost.self,from:data!) self.site = shost DispatchQueue.main.async{ //back to main } }catch{ print(error.localizedDescription); } }) task.resume() } } }