1importUIKit23classViewController:UIViewController{45overridefuncviewDidLoad(){6super.viewDidLoad()7// Do any additional setup after loading the view, typically from a nib.89guardlet urlString =URL(string:"https://httpbin.org/post")else{10return11}1213let parameters:[String:Any]=[14"Diary":[15"ID":1,16"Title":"hoge",17"Content":"fuga",18"XTime":"2018/01/03 12:34:56",19"IsShared":"no"20]]2122var request =URLRequest(url: urlString)23 request.httpMethod ="POST"24 request.timeoutInterval =1025 request.addValue("application/json", forHTTPHeaderField:"Content-Type")2627do{28 request.httpBody =tryJSONSerialization.data(withJSONObject: parameters, options:.prettyPrinted)29}catch{30print(error)31}3233let task =URLSession.shared.dataTask(with: request, completionHandler:{data, response, error in3435iflet data = data,let response = response {36print(String(data: data, encoding:.utf8))37// print(response)38do{39let json =tryJSONSerialization.jsonObject(with: data, options:.allowFragments)40print(json)41}catch{42print(error)43}44}else{45print(error ??"error")46}47})48 task.resume()4950}5152overridefuncdidReceiveMemoryWarning(){53super.didReceiveMemoryWarning()54// Dispose of any resources that can be recreated.55}565758}