Last active
November 7, 2021 15:13
-
-
Save martincalvert/e5e8fc2bf711f08e54a81683133caf6f to your computer and use it in GitHub Desktop.
How to run URLSession requests in the playground in XCode 8.3
This file contains 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
//: 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