Swift 5.5: Async/Await URLSession.shared.data() throws an error

Change http to https. You cannot normally call insecure calls unless you add an exception to your Info.plist to disable App Transport Security.

There should be also a log from App Transport Security in your console.

Since ATS blocks the connection, the request gets cancelled.

See NSAppTransportSecurity for more info


I encountered the same problem in a SwiftUI app. I was using the task view modifier to load data asynchronously and update the view based on the loading states.

As mentioned in the documentation:

If the task doesn’t finish before SwiftUI removes the view or the view changes identity, SwiftUI cancels the task.

If this is your case, make sure to update the UI only after the task completes.

Alternative solution

Alternatively you can use the onAppear view modifier (which doesn't cancel the Task) and run the task inside its body:

Color.clear.onAppear {
    Task {
        await viewModel.load()
    }
}