How to make an HTTP request in Swift: a step-by-step guide for iOS developers
Hello! I'm a beginner in Swift and iOS development, trying to make a simple GET request to an API, but nothing works. My code looks like this:
```swift
let url = URL(string: "https://api.example.com/users")
let request = URLRequest(url: url!)
let dataTask = URLSession.shared.dataTask(with: request) { data, response, error in
print(data)
}
dataTask.resume()
```
The console prints `nil`. I don't understand why the data isn't coming through. I also tried using `URLSession.shared.dataTask(with: url)` — same result.
I read that I need to use `URLSession`, but the examples online are too complicated: there are closures, error handling, JSONDecoder... I just need to get a string from the server and print it.
Please tell me how to properly make an HTTP request in Swift? Preferably
No answers yet.
Log in to leave an answer