Skip to main content
3 of 4
edited tags; edited title
Glorfindel
  • 6.9k
  • 3
  • 22
  • 49

Can't Decompress Stack Overflow API in Swift

I am using the Stack Overflow API in Swift but keep getting an error:

The data couldn’t be read because it isn’t in the correct format.

whenever I try to use the JSON as shown in code below

func api(text: String){
    print(text)
    if let url = URL(string: "https://api.stackexchange.com/docs/search#order=desc&sort=activity&filter=default&site=stackoverflow") {
        var request = URLRequest(url: url)
        request.addValue("intitle", forHTTPHeaderField: text)
        request.httpMethod = "GET"
        print(request.description)
        let dataTask = URLSession.shared.dataTask(with: request) { (data: Data?, response: URLResponse?, error: Error?) in
            //handle response here
            if let error = error {
                
                return
            }
            
            do {
                
                let json = try JSONSerialization.jsonObject(with: data!) as! [String: Any]
                print(json)
                
            } catch {
                print(error.localizedDescription)
            }
        }
        dataTask.resume()
    }
}

Any advice on what I'm doing wrong? I saw somewhere that the response was in GZip format so I tried using Alamofire because it should have some decompression for it, I thought, but still same issue.