MΓ³dulo 07 Β· Talking to the Backend β LecciΓ³n 1 de 4 Β· ~12 min
Your First Network Request
Your app is about to make its first call
Everything real in PawWalk β the walkers, your bookings, your login β lives on the backend. Until now you've decoded JSON that was handed to you as Data. This module is about getting that Data yourself, over the network. Four words to pin down first:
- URL β the address of one thing on a server, like
http://localhost:8000/walkers. In Swift it's a type:URL. - Request β the full ask you send: a URL plus how you're asking (fetch it? create something?) plus any extra info. Swift's type is
URLRequest. - Response β what comes back: some metadata ("did it work?") and a body of raw bytes (usually JSON).
- URLSession β Apple's networking engine.
URLSession.sharedis a ready-made instance the whole app can use.
The heart of it all is one line:
let (data, response) = try await URLSession.shared.data(for: request)
It's async β your code pauses here without freezing the app (exactly the await you learned earlier) β and it can throw, because networks fail: airplane mode, backend not running, Wi-Fi drops. One line in, you get two things back: the body bytes and the metadata.