Sending an HTTP POST request on iOS
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
On iOS, the standard way to send an HTTP POST request is with URLSession. The job has three parts: build a URLRequest, attach the body and headers, then send it asynchronously and validate the response before decoding or using the returned data.
Build the Request Explicitly
A POST request needs a URL, HTTP method, headers, and body. JSON is the most common payload format for mobile APIs.
Being explicit keeps the request easy to inspect when debugging server-side failures.
Send the Request With URLSession
Modern Swift code usually uses async and await with URLSession.shared.data(for:).
This pattern is clear and correct for most application code:
- build request
- await response
- validate status code
- decode payload
Call It From the UI Safely
Networking is asynchronous, so call it from a task instead of blocking the main thread.
The UI stays responsive because URLSession does not block the main thread while the request is in flight.
Posting Form Data Instead of JSON
Not every API expects JSON. Some older endpoints or OAuth flows expect application/x-www-form-urlencoded.
The transport is still HTTP POST. What changes is the body encoding and content type.
Reuse a Configured Session When Needed
For apps with authentication, retry behavior, custom timeouts, or background transfer requirements, create a dedicated URLSession with a URLSessionConfiguration instead of always using shared.
This is especially useful when the app talks to one API consistently and needs stable networking policy.
Inspect the Response, Not Just the Error
A failed API call often returns a perfectly valid HTTP response with a 400, 401, or 500 status. That is not the same as a transport failure.
A robust POST implementation should distinguish between:
- network transport errors such as no connectivity
- HTTP status failures returned by the server
- decoding failures caused by unexpected response formats
Lumping them together makes troubleshooting much harder.
App Transport Security Still Applies
On iOS, plain HTTP endpoints may be blocked by App Transport Security unless you configure exceptions. In most modern apps, the correct solution is simply to use HTTPS.
If a request mysteriously fails before reaching the server, confirm that the URL scheme and ATS policy are compatible.
Common Pitfalls
A common mistake is forgetting to set httpMethod = "POST". The request then defaults to GET even if the body exists.
Another mistake is sending JSON bytes without setting the Content-Type header. Some servers accept it anyway, but many will not.
Developers also skip status-code validation and try to decode every response as if it were a successful payload.
Finally, do not run synchronous network patterns on the main thread. URLSession is designed for asynchronous use, and iOS networking code should stay that way.
Summary
- Use
URLRequestplusURLSessionto send HTTP POST requests on iOS. - Set the method, headers, and body explicitly.
- Use
asyncandawaitfor clear non-blocking networking code. - Validate the HTTP status before decoding the response body.
- Match the body encoding and
Content-Typeto what the server expects.

