AFNetworking Post Request
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
AFNetworking is still common in older Objective-C iOS and macOS projects even though newer apps often use URLSession directly. A solid POST request with AFNetworking comes down to matching the server's expected body format, configuring serializers deliberately, and handling failure details instead of treating every network error the same way.
Basic POST Request with AFHTTPSessionManager
The usual entry point is AFHTTPSessionManager. For form-style API input, the default request serializer is often enough.
With the default serializer, AFNetworking sends parameters as application/x-www-form-urlencoded. That is correct for many older APIs and login endpoints, but it is wrong for APIs that expect JSON.
Sending JSON in the Request Body
If the server expects JSON, switch the request serializer before sending the request. That changes both the body format and the Content-Type header.
This is one of the most common AFNetworking mistakes: the code looks correct, but the body format does not match what the server expects. That usually turns into 400 or 415 responses.
Inspecting Status Codes and Error Context
A generic error log is not enough when debugging real API problems. In failure blocks, it is often useful to inspect the HTTP response object as well.
This helps you separate transport failures from application-level failures such as 401, 403, or 422. It also helps when the backend returns a structured error payload but the top-level message alone is too vague to be useful.
Multipart POST for File Uploads
If the request includes an image or document, use AFNetworking's multipart API instead of trying to build the body manually.
The multipart helper handles boundaries and file metadata correctly, which is exactly the sort of boilerplate you do not want to hand-roll.
Headers and Shared Configuration
Authentication and content negotiation usually belong in headers. If the same token is needed across many requests, set it on the serializer once.
If only one request needs a special header, pass it in the headers: argument instead. That keeps per-request behavior explicit and avoids surprising side effects on unrelated calls.
Common Pitfalls
- Using the default form serializer when the backend expects JSON.
- Ignoring the HTTP status code and logging only
NSError.localizedDescription. - Forgetting to widen
acceptableContentTypeswhen the backend returns JSON with an unexpected content type. - Uploading files through a normal parameter dictionary instead of the multipart API.
- Capturing
selfstrongly inside success and failure blocks in view controllers that retain the manager.
Summary
- '
AFHTTPSessionManageris the standard AFNetworking entry point forPOSTrequests.' - Match the request serializer to the server's expected body format.
- Inspect status codes and response details when handling failures.
- Use multipart helpers for file uploads instead of building the body manually.
- Treat headers, serializers, and error handling as part of the API contract, not optional extras.

