I am trying to do a get request using the stackoverflow api using URLSession but i keep getting the following error:
The data couldn’t be read because it isn’t in the correct format.
override func viewDidLoad() {
super.viewDidLoad()
searchBar.delegate = self
searchBar.sendsWholeSearchString = true
if let url = URL(string: "https://api.stackexchange.com/docs/search#order=desc&sort=activity&intitle=swift&filter=default&site=stackoverflow") {
var request = URLRequest(url: url)
request.httpMethod = "GET"
let dataTask = URLSession.shared.dataTask(with: request) { (data: Data?, response: URLResponse?, error: Error?) in
//handle response here
if let error = error {
print("Fuck! \(error.localizedDescription)")
return
}
do {
let json = try JSONSerialization.jsonObject(with: data!) as! Dictionary<String, AnyObject>
print(json)
} catch {
print(error.localizedDescription)
}
}
dataTask.resume()
}
}
I already have the url set to get swift results as if from a search so the url is "https://api.stackexchange.com/docs/search#order=desc&sort=activity&intitle=swift&filter=default&site=stackoverflow"
Also I read that the data is normally a gzip format but found that apparently URLSession automatically handles it ..
Any suggestions on where i am going wrong?
print(String.init(data: data!, encoding: .utf8)!).