Skip to content

Instantly share code, notes, and snippets.

@leovolving
Created September 11, 2017 01:39
Show Gist options
  • Save leovolving/f4e9586506c61941410bca83e5900f24 to your computer and use it in GitHub Desktop.
Save leovolving/f4e9586506c61941410bca83e5900f24 to your computer and use it in GitHub Desktop.
GET Request Using Swift
//A basic GET request using the Ron Swanson Quote API
// https://github.com/jamesseanwright/ron-swanson-quotes
let targetUrl = URL(string: "http://ron-swanson-quotes.herokuapp.com/v2/quotes")
let task = URLSession.shared.dataTask(with: targetUrl!) { (data, response, error) in
if error != nil {
print("ERROR!")
}
else {
if let content = data {
do {
let myData = try JSONSerialization.jsonObject(with: content, options: JSONSerialization.ReadingOptions.mutableContainers) as AnyObject
print(myData)
}
catch {
print("ERROR.")
}
}
}
}
task.resume()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment