Swift
iOS
NSURLRequest
HTTP POST
app development

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:

swift
1import Foundation
2
3let url = URL(string: "https://example.com/api/login")!
4var request = URLRequest(url: url)
5request.httpMethod = "POST"

At this point the request exists, but it does not yet have a body.

Sending JSON data

Most modern APIs expect JSON.

swift
1import Foundation
2
3let url = URL(string: "https://example.com/api/login")!
4var request = URLRequest(url: url)
5request.httpMethod = "POST"
6request.setValue("application/json", forHTTPHeaderField: "Content-Type")
7
8let payload: [String: Any] = [
9    "email": "[email protected]",
10    "password": "secret"
11]
12
13request.httpBody = try JSONSerialization.data(withJSONObject: payload)
14
15let task = URLSession.shared.dataTask(with: request) { data, response, error in
16    if let error = error {
17        print("Request failed:", error)
18        return
19    }
20    print("Request completed")
21}
22
23task.resume()

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.

swift
1import Foundation
2
3let url = URL(string: "https://example.com/api/login")!
4var request = URLRequest(url: url)
5request.httpMethod = "POST"
6request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
7
8let body = "[email protected]&password=secret"
9request.httpBody = body.data(using: .utf8)

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.

objective-c
1NSURL *url = [NSURL URLWithString:@"https://example.com/api/login"];
2NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
3[request setHTTPMethod:@"POST"];
4[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
5
6NSData *body = [@"{\"email\":\"[email protected]\"}" dataUsingEncoding:NSUTF8StringEncoding];
7[request setHTTPBody:body];

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 POST and 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, use NSMutableURLRequest.
  • Headers describe the payload, but they do not replace the payload itself.
  • Choose the body encoding to match the server contract exactly.

Course illustration
Course illustration

All Rights Reserved.