AFNetworking 2.0 add headers to GET request
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In AFNetworking 2.0, request headers are usually set on the request serializer before the request is sent. For a GET request, that means you do not add headers in the URL itself or in the response serializer. You configure the requestSerializer on the manager, or you build a custom request and attach headers there.
The Normal AFNetworking 2.0 Pattern
The most common object in AFNetworking 2.0 is AFHTTPRequestOperationManager. It exposes a requestSerializer, and that serializer owns the HTTP header fields that will be applied to outgoing requests.
A simple example:
Those headers are attached to the outgoing GET request because they were configured on the request serializer before the request started.
Headers Set on the Manager Affect Future Requests
A useful detail is that the serializer belongs to the manager. If you reuse the same manager for several requests, any headers you set remain there until changed or cleared.
That is convenient for shared headers such as:
- '
Authorization' - '
Accept' - '
User-Agent' - custom API version headers
It also means you should be careful when one request needs special headers but another should not inherit them.
Per-Request Header Customization
If you need headers for only one request, you can create an NSMutableURLRequest through the serializer and then run it with an operation.
This pattern is safer when one request needs temporary headers and you do not want those headers to leak into later calls made by the same manager.
Choosing the Right Serializer
The request serializer controls how parameters and headers are encoded. AFNetworking 2.0 commonly uses:
- '
AFHTTPRequestSerializerfor standard form-style requests' - '
AFJSONRequestSerializerwhen the request body should be JSON'
For GET requests, parameters usually go in the query string either way, but headers are still configured through the serializer.
Example:
Even though Content-Type is more relevant for body-bearing requests, the general header-setting mechanism is the same.
Debugging Header Problems
If the server says an auth header is missing, check these points:
- was the header set before the request started
- are you using the same manager instance you configured
- did later code overwrite the header value
- is a proxy or test server showing the raw outgoing request
It can help to inspect the built request directly:
That gives you a direct view of what AFNetworking is about to send.
Common Pitfalls
The most common mistake is trying to set headers on the response serializer instead of the request serializer. Another is setting a header on a shared manager and then forgetting that all later requests through that manager inherit it. Developers also sometimes add headers after the request has already been created or started, which has no effect on the already-built request. A final issue is using the convenience GET method when a one-off custom request would be cleaner for per-request headers.
Summary
- In AFNetworking 2.0, headers for a
GETrequest are usually set onmanager.requestSerializer. - Shared headers belong on the manager's serializer before the request starts.
- One-off headers are better handled by building a custom
NSMutableURLRequest. - The response serializer does not control outgoing headers.
- If headers seem missing, inspect the actual request object and the lifetime of the manager configuration.

