Skip to content

Instantly share code, notes, and snippets.

@Melonbwead
Created October 31, 2016 11:10
Show Gist options
  • Save Melonbwead/acebd4546257edfae2ead48641ea9962 to your computer and use it in GitHub Desktop.
Save Melonbwead/acebd4546257edfae2ead48641ea9962 to your computer and use it in GitHub Desktop.
// prepare json data
let mapDict = [ "1":"First", "2":"Second"]
let json = [ "title":"ABC" , "dict": mapDict ] as [String : Any]
let jsonData = try? JSONSerialization.data(withJSONObject: json, options: .prettyPrinted)
// create post request
let url = NSURL(string: "https://httpbin.org/post")!
let request = NSMutableURLRequest(url: url as URL)
request.httpMethod = "POST"
// insert json data to the request
request.httpBody = jsonData
let task = URLSession.shared.dataTask(with: request as URLRequest){ data,response,error in
if error != nil{
print(error?.localizedDescription)
return
}
if let responseJSON = try? JSONSerialization.jsonObject(with: data!) as? [String:AnyObject]{
print(responseJSON)
}
}
task.resume()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment