Upload image with parameters in Swift
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
Uploading an image together with extra fields in Swift usually means sending a multipart/form-data request. The server receives one part for the image bytes and separate parts for text parameters such as user IDs, captions, or flags.
Build a Multipart Request
The main pieces are:
- a
URLRequest - a unique multipart boundary
- text fields appended as form parts
- image data appended as a file part
Here is a complete example with URLSession:
This function creates the body manually, which is often the simplest pure-Foundation solution.
Call It from App Code
Example usage:
In a real app, the image usually comes from the camera, photo library, or an edited image pipeline rather than a system symbol.
Why Multipart Is Required
A JSON request is not the normal format for raw image upload because binary image bytes and text form values need different treatment. multipart/form-data solves that by separating each field into a distinct part with its own headers.
That is why the boundary matters. It marks where one field ends and the next begins. If the boundary or line endings are wrong, the server will often reject the request even though the code compiles.
Choose the Right Image Encoding
jpegData(compressionQuality:) is appropriate for photos. For images that need transparency, such as stickers or icons, PNG may be more appropriate:
The server and the declared MIME type must match the actual bytes you send.
Handle Server Contracts Explicitly
The field names in your multipart body must match what the server expects. If the backend expects file instead of image, or user_id instead of userId, the request can succeed technically while still failing semantically.
Good upload code is not just about HTTP syntax. It also has to match the backend contract exactly.
Common Pitfalls
- Forgetting the closing boundary at the end of the multipart body.
- Declaring
image/jpegwhile actually sending PNG data, or the reverse. - Using parameter names that do not match the server contract.
- Ignoring non-2xx status codes and treating any response body as success.
- Building the request correctly but uploading an image representation that is unexpectedly large for mobile networks.
Summary
- To upload an image with extra fields in Swift, use a
multipart/form-datarequest. - Build the request body with a boundary, text parameters, and a file part for the image.
- Make sure the MIME type matches the actual image encoding.
- Validate the HTTP status code instead of assuming the upload succeeded.
- Server field names and multipart formatting must match the backend contract exactly.
Related reading
- Uploading file with parameters using Alamofire
- uri for aws_api_gateway_integration for type AWS integration to DynamoDb
- Use host networking while building a VSCode devcontainer
- Use of PUT vs PATCH methods in REST API real life scenarios
- Uploading archive error Missing iOS Distribution signing identity for ...
- URL encoding in Android
- Use RestTemplate with object as data and application/x-www-form-urlencoded content type?
- Use Tensorflow Object Detection API to detect small objects in images

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.