Swift GET request with parameters
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
In Swift, a GET request with parameters is usually built by adding query items to the URL rather than by placing data in the HTTP body. The safest way to do that is with URLComponents, because it handles percent-encoding correctly. Once the URL is built, URLSession performs the request in the normal way.
Build the URL with URLComponents
Start by creating the base URL and attaching query items:
This is better than manual string concatenation because special characters in parameter values are encoded correctly.
Execute the GET Request with URLSession
With async and await, the request code is compact and readable:
The query parameters are part of the URL, while the request method stays GET.
Decode JSON into a Swift Type
In real apps, you usually decode the response rather than using raw Data.
This keeps the networking layer typed and easier to maintain.
Completion-Handler Version
If you are working in older code that does not use async and await yet, the same request can be made with dataTask:
If you need to update the UI afterward, hop back to the main thread.
Why GET Parameters Belong in the URL
By convention and in practice, GET requests carry their parameters in the query string. Servers, caches, and proxies expect GET requests to be safe and URL-addressable. If the data is large, sensitive, or conceptually a request body, a POST request is usually the better fit.
That distinction also helps prevent a common bug: trying to attach httpBody to a GET request and expecting every server or middleware layer to treat it consistently.
Common Pitfalls
One common mistake is building the URL by hand with string concatenation. That often breaks when values contain spaces, slashes, or other characters that need encoding.
Another mistake is sending GET parameters in httpBody instead of using query items. Even if some server accepts it, it is not the normal or most interoperable approach.
Developers also sometimes ignore the HTTP status code and decode whatever came back. Always validate the response before assuming the data matches your model.
Finally, remember that URLSession callbacks do not update the UI on the main thread automatically. If the response changes visible state, switch back to the main actor or main queue.
Summary
- Build GET parameters with
URLComponentsandURLQueryItem. - Use
URLSessionto perform the request after the URL is assembled. - Put GET parameters in the query string, not the request body.
- Check HTTP status codes before decoding the response.
- Prefer typed decoding with
Decodablefor maintainable networking code.
Related reading
- Swift GET request with parameters
- Swift how to use PREPROCESSOR Flags like if DEBUG to implement API keys?
- Sync data between Android App and webserver
- Synchronization in distributed processes
- Swift guard let vs if let
- Swift How to get substring from start to last index of character
- Synchronize actions in a distributed system
- Synchronous and Asynchronous data transmission between client and server

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.