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 should usually be built with URLComponents and URLQueryItem. That approach is safer than concatenating query strings by hand because encoding is handled automatically and the resulting URL is easier to inspect and test.
Building the URL correctly
Suppose an endpoint accepts a search term, a page number, and a sort option. The safest construction is:
This matters because spaces, ampersands, and other reserved characters must be percent-encoded correctly. URLComponents does that work for you.
Sending the request with URLSession
Once the URL is built, you can fetch data with Swift concurrency:
The request stays readable, and the decoding logic is separate from the URL assembly.
Adding headers and request options
Even for a GET request, you may need custom headers such as Accept or authentication:
That pattern scales better than calling data(from:) once request customization becomes necessary.
Reusing query-building logic
If your app calls several endpoints, centralize the construction logic in a small client:
This keeps callers focused on endpoint intent instead of repetitive encoding logic.
Common Pitfalls
The biggest mistake is manual string concatenation such as "https://... ?q=" + term. That tends to break when values contain spaces, slashes, ampersands, or non-ASCII text.
Another issue is treating all non-200 responses the same without checking the actual status code range or server payload. Real APIs often return meaningful error bodies.
People also mix URL construction, networking, and JSON decoding into one long method. That works for one request and becomes difficult to test after the third or fourth endpoint.
Finally, remember that GET parameters belong in the URL query, not in the HTTP body. Some servers ignore GET bodies entirely.
Summary
- Use
URLComponentsandURLQueryItemto build GET requests with parameters in Swift. - Use
URLSessionto send the request and validate the HTTP status code explicitly. - Switch to
URLRequestwhen you need headers, timeouts, or other request customization. - Keep URL-building logic reusable instead of hand-writing query strings everywhere.
- Let the standard library handle encoding instead of doing it manually.
Related reading
- Swift how to use PREPROCESSOR Flags like if DEBUG to implement API keys?
- Sync data between Android App and webserver
- Synchronization in distributed processes
- Synchronize actions in a distributed system
- Swift guard let vs if let
- Swift How to get substring from start to last index of character
- Synchronous and Asynchronous data transmission between client and server
- System.AggregateException on Socket.EndAccept with TaskFactory.FromAsync

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.