Skip to main content
1 of 4

Cant Decompress StackOverflow APi in Swift

I am using the Stackoverflow 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 im 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.