Avatar ·

How to make an HTTP request in Swift?

Hello everyone! Please advise how to properly perform an HTTP request in Swift for an iOS/macOS application. I'm new to Apple development and don't fully understand which approach is currently considered relevant.

I need to send a GET request to a public REST API (for example, to get a list of users) and process the response in JSON format. I tried using the old URLSession with delegates, but the code turns out to be cumbersome and hard to understand. I also came across third-party libraries like Alamofire, but I'd like to first figure out native tools.

Here's an example of my current code that doesn't work correctly (error when decoding JSON):

let url = URL(string: "https://jsonplaceholder.typicode.com/users")!\nlet task = URLSession.shared.da