-2

Hi I'm trying to retrieve data from url. So I'm using dataTask with url.

THe response received is showing as 0 bytes but the I print it shown around 46171 bytes of data in upperbound...please find below screen shot. Because of this im not able to parse json

PLease advice what needs to be done in this case

enter image description here

Ashh
  • 569
  • 1
  • 6
  • 28

1 Answers1

0

If you want to get the results to do anything useful, then you need to implement the completionHandler.

Also, if you want to access the URL (from the urlRequest) and obtain the results of a GET request, then you need to add task.resume().

I hope the following example helps:

let urlRequest = URLRequest(url: url)
let session = URLSession.shared
let task = session.dataTask(with: urlRequest, completionHandler:{ (data: Data?, response: URLResponse?, error: Error?) in
    if let response = response {
        print(response)
    }
    if let error = error { 
        print(error)
    }   
}) 
    task.resume()