Search code examples
swiftapiurlsession

How can I get the Responses Body in URLSession Swift?


) I'm trying to get the Error messages from the response's body to post request. Here is the example of my code:

let task = URLSession.shared.dataTask(with: request) { _, response, error in
            if let error = error {
                print("Error took place \(error)")
            } else {
                print(response.?????)
            }

What should I do to see the responses body?


Solution

  • You are ignoring the first parameter in the closure (_, response, error), and actually there is your data. Check the Apple sample to understand more https://developer.apple.com/documentation/foundation/url_loading_system/fetching_website_data_into_memory