Skip to content

Instantly share code, notes, and snippets.

@martincalvert
Last active November 7, 2021 15:13
Show Gist options
  • Save martincalvert/e5e8fc2bf711f08e54a81683133caf6f to your computer and use it in GitHub Desktop.
Save martincalvert/e5e8fc2bf711f08e54a81683133caf6f to your computer and use it in GitHub Desktop.
How to run URLSession requests in the playground in XCode 8.3
//: Playground - noun: a place where people can play
import Foundation
import PlaygroundSupport
PlaygroundPage.current.needsIndefiniteExecution = true
var request = URLRequest(url: URL(string: "https://api.github.com/")!)
request.httpMethod = "GET"
let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data, error == nil else {
print("error=\(error)")
return
}
if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 {
print("statusCode should be 200, but is (httpStatus.statusCode)")
print("response = \(response)")
}
if let body_response = String(data: data, encoding: String.Encoding.utf8) {
print(body_response)
}
}
task.resume()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment