I'm trying to learn how to use URLSession, so I'm trying to get the raw JSON from a URL as a consistency check that it worked. But I don't understand why it's outputting the data responses size in bytes instead of the actual data
Here's an example that I found online and modified to try and get it to return the JSON:
import UIKit
import PlaygroundSupport
PlaygroundPage.current.needsIndefiniteExecution = true
let url = URL(string: "http://date.jsontest.com/")!
let session = URLSession.shared
let q = session.dataTask(with: url) { data, response, error in
do {
print(data!)
}
catch {
print("Error \(error)")
}
}
q.resume()
This code returns something similar to
100 bytes
, rather than the JSON itself. It doesn't make any sense, Apple's documentation says that data
is "The data returned by the server.", so why is it returning the size of the data instead of the JSON of the URL?