Created
October 31, 2016 11:10
-
-
Save Melonbwead/acebd4546257edfae2ead48641ea9962 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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