Skip to content

Instantly share code, notes, and snippets.

@AshishKapoor
Last active May 11, 2017 15:40
Show Gist options
  • Save AshishKapoor/d48f830a21e50cfe44201de0da297c3a to your computer and use it in GitHub Desktop.
Save AshishKapoor/d48f830a21e50cfe44201de0da297c3a to your computer and use it in GitHub Desktop.
Swift 3.1 Get Request
let url = URL(string: "https://api.themoviedb.org/3/discover/tv?" +
"include_null_first_air_dates=false&timezone=America%2FNew_York" +
"&page=1&sort_by=popularity.desc&language=en-US" +
"&api_key=<api_key>")
var request = URLRequest(url: url!, cachePolicy: .useProtocolCachePolicy, timeoutInterval: 10.0)
request.httpMethod = "GET"
request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
let session = URLSession.shared
let dataTask = session.dataTask(with: request, completionHandler: { (data, response, error) -> Void in
if (error != nil) {
print(error!)
} else {
let json = try? JSONSerialization.jsonObject(with: data!, options: [])
print(json!)
}
})
dataTask.resume()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment