Append data to a POST NSURLRequest
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
You do not append data to an immutable NSURLRequest directly. On Apple platforms, the normal pattern is to create a mutable request, set the HTTP method to POST, encode the payload in the format the server expects, and assign that data to the HTTP body.
Use a mutable request
In Objective-C, that means NSMutableURLRequest. In Swift, the modern type is URLRequest, which is a value type you can mutate before sending.
Swift example:
At this point the request exists, but it does not yet have a body.
Sending JSON data
Most modern APIs expect JSON.
The key step is assigning encoded Data to request.httpBody.
Sending form-encoded data
Older or simpler endpoints may expect application/x-www-form-urlencoded instead of JSON.
This works, but it is your responsibility to percent-encode values correctly if they may contain reserved characters.
Objective-C with NSMutableURLRequest
If you are working with NSURLRequest terminology directly, you may be in Objective-C code.
That is the same idea in Cocoa-style APIs: use the mutable request type and assign the body explicitly.
Multipart uploads are different
If the POST body includes files, images, or mixed form fields, you usually need multipart/form-data, which has a different body format with boundaries.
That is why many examples online appear more complicated than a basic JSON POST. They are solving a different body-format problem, not showing a better way to send ordinary structured data.
That is not something you build by blindly appending bytes unless you understand the format exactly. Many networking libraries or helpers exist specifically because multipart encoding is easy to get wrong.
Do not confuse headers with body content
A common mistake is setting the content type and assuming the data is now somehow attached. Headers only describe the body. They do not create it.
It is also worth remembering that many server-side errors blamed on networking code are really body-format mismatches. The request can be transmitted correctly while still being rejected because the payload does not match the declared format.
Similarly, setting httpMethod = "POST" does not automatically add any payload. The body must be encoded and assigned explicitly.
Common Pitfalls
A common mistake is trying to modify NSURLRequest instead of NSMutableURLRequest or a mutable Swift URLRequest.
Another mistake is sending JSON data without setting Content-Type: application/json.
A third mistake is building form-encoded bodies by string concatenation without proper percent encoding.
Summary
- Use a mutable request type when building a POST request.
- Set the method to
POSTand assign data to the HTTP body. - Match the body format to what the server expects, usually JSON or form encoding.
- In Swift, use
URLRequest; in Objective-C, useNSMutableURLRequest. - Headers describe the payload, but they do not replace the payload itself.
- Choose the body encoding to match the server contract exactly.

