Inherits from AFURLSessionManager : NSObject
Conforms to NSCopying
NSSecureCoding
Declared in AFHTTPSessionManager.h

Overview

AFHTTPSessionManager is a subclass of AFURLSessionManager with convenience methods for making HTTP requests. When a baseURL is provided, requests made with the GET / POST / et al. convenience methods can be made with relative paths.

Subclassing Notes

Developers targeting iOS 7 or Mac OS X 10.9 or later that deal extensively with a web service are encouraged to subclass AFHTTPSessionManager, providing a class method that returns a shared singleton object on which authentication and other configuration can be shared across the application.

For developers targeting iOS 6 or Mac OS X 10.8 or earlier, AFHTTPRequestOperationManager may be used to similar effect.

Methods to Override

To change the behavior of all data task operation construction, which is also used in the GET / POST / et al. convenience methods, override dataTaskWithRequest:completionHandler:.

Serialization

Requests created by an HTTP client will contain default headers and encode parameters according to the requestSerializer property, which is an object conforming to AFURLRequestSerialization.

Responses received from the server are automatically validated and serialized by the responseSerializers property, which is an object conforming to AFURLResponseSerialization

URL Construction Using Relative Paths

For HTTP convenience methods, the request serializer constructs URLs from the path relative to the baseURL, using NSURL +URLWithString:relativeToURL:, when provided. If baseURL is nil, path needs to resolve to a valid NSURL object using NSURL +URLWithString:.

Below are a few examples of how baseURL and relative paths interact:

NSURL *baseURL = [NSURL URLWithString:@"http://example.com/v1/"];
[NSURL URLWithString:@"foo" relativeToURL:baseURL];                  // http://example.com/v1/foo
[NSURL URLWithString:@"foo?bar=baz" relativeToURL:baseURL];          // http://example.com/v1/foo?bar=baz
[NSURL URLWithString:@"/foo" relativeToURL:baseURL];                 // http://example.com/foo
[NSURL URLWithString:@"foo/" relativeToURL:baseURL];                 // http://example.com/v1/foo
[NSURL URLWithString:@"/foo/" relativeToURL:baseURL];                // http://example.com/foo/
[NSURL URLWithString:@"http://example2.com/" relativeToURL:baseURL]; // http://example2.com/

Also important to note is that a trailing slash will be added to any baseURL without one. This would otherwise cause unexpected behavior when constructing URLs using paths without a leading slash.

Warning: Managers for background sessions must be owned for the duration of their use. This can be accomplished by creating an application-wide or shared singleton instance.

Tasks

Other Methods

  •   baseURL

    The URL used to construct requests from relative paths in methods like requestWithMethod:URLString:parameters:, and the GET / POST / et al. convenience methods.

    property
  •   requestSerializer

    Requests created with requestWithMethod:URLString:parameters: & multipartFormRequestWithMethod:URLString:parameters:constructingBodyWithBlock: are constructed with a set of default headers using a parameter serialization specified by this property. By default, this is set to an instance of AFHTTPRequestSerializer, which serializes query string parameters for GET, HEAD, and DELETE requests, or otherwise URL-form-encodes HTTP message bodies.

    property
  •   responseSerializer

    Responses sent from the server in data tasks created with dataTaskWithRequest:success:failure: and run using the GET / POST / et al. convenience methods are automatically validated and serialized by the response serializer. By default, this property is set to an instance of AFJSONResponseSerializer.

    property

Initialization

Making HTTP Requests

Properties

baseURL

@property (readonly, nonatomic, strong, nullable) NSURL *baseURL
Discussion

The URL used to construct requests from relative paths in methods like requestWithMethod:URLString:parameters:, and the GET / POST / et al. convenience methods.

Declared In

AFHTTPSessionManager.h

requestSerializer

@property (nonatomic, strong) AFHTTPRequestSerializer<AFURLRequestSerialization> *requestSerializer
Discussion

Requests created with requestWithMethod:URLString:parameters: & multipartFormRequestWithMethod:URLString:parameters:constructingBodyWithBlock: are constructed with a set of default headers using a parameter serialization specified by this property. By default, this is set to an instance of AFHTTPRequestSerializer, which serializes query string parameters for GET, HEAD, and DELETE requests, or otherwise URL-form-encodes HTTP message bodies.

Warning: requestSerializer must not be nil.

Declared In

AFHTTPSessionManager.h

responseSerializer

@property (nonatomic, strong) AFHTTPResponseSerializer<AFURLResponseSerialization> *responseSerializer
Discussion

Responses sent from the server in data tasks created with dataTaskWithRequest:success:failure: and run using the GET / POST / et al. convenience methods are automatically validated and serialized by the response serializer. By default, this property is set to an instance of AFJSONResponseSerializer.

Warning: responseSerializer must not be nil.

Declared In

AFHTTPSessionManager.h

Class Methods

manager

+ (instancetype)manager
Discussion

Creates and returns an AFHTTPSessionManager object.

Declared In

AFHTTPSessionManager.h

Instance Methods

DELETE:parameters:success:failure:

- (nullable NSURLSessionDataTask *)DELETE:(NSString *)URLString parameters:(nullable id)parameters success:(nullable void ( ^ ) ( NSURLSessionDataTask *task , id responseObject ))success failure:(nullable void ( ^ ) ( NSURLSessionDataTask *task , NSError *error ))failure
Discussion

Creates and runs an NSURLSessionDataTask with a DELETE request.

Parameters

URLString

The URL string used to create the request URL.

parameters

The parameters to be encoded according to the client request serializer.

success

A block object to be executed when the task finishes successfully. This block has no return value and takes two arguments: the data task, and the response object created by the client response serializer.

failure

A block object to be executed when the task finishes unsuccessfully, or that finishes successfully, but encountered an error while parsing the response data. This block has no return value and takes a two arguments: the data task and the error describing the network or parsing error that occurred.

Declared In

AFHTTPSessionManager.h

GET:parameters:success:failure:

- (nullable NSURLSessionDataTask *)GET:(NSString *)URLString parameters:(nullable id)parameters success:(nullable void ( ^ ) ( NSURLSessionDataTask *task , id responseObject ))success failure:(nullable void ( ^ ) ( NSURLSessionDataTask *task , NSError *error ))failure
Discussion

Creates and runs an NSURLSessionDataTask with a GET request.

Parameters

URLString

The URL string used to create the request URL.

parameters

The parameters to be encoded according to the client request serializer.

success

A block object to be executed when the task finishes successfully. This block has no return value and takes two arguments: the data task, and the response object created by the client response serializer.

failure

A block object to be executed when the task finishes unsuccessfully, or that finishes successfully, but encountered an error while parsing the response data. This block has no return value and takes a two arguments: the data task and the error describing the network or parsing error that occurred.

Declared In

AFHTTPSessionManager.h

HEAD:parameters:success:failure:

- (nullable NSURLSessionDataTask *)HEAD:(NSString *)URLString parameters:(nullable id)parameters success:(nullable void ( ^ ) ( NSURLSessionDataTask *task ))success failure:(nullable void ( ^ ) ( NSURLSessionDataTask *task , NSError *error ))failure
Discussion

Creates and runs an NSURLSessionDataTask with a HEAD request.

Parameters

URLString

The URL string used to create the request URL.

parameters

The parameters to be encoded according to the client request serializer.

success

A block object to be executed when the task finishes successfully. This block has no return value and takes a single arguments: the data task.

failure

A block object to be executed when the task finishes unsuccessfully, or that finishes successfully, but encountered an error while parsing the response data. This block has no return value and takes a two arguments: the data task and the error describing the network or parsing error that occurred.

Declared In

AFHTTPSessionManager.h

PATCH:parameters:success:failure:

- (nullable NSURLSessionDataTask *)PATCH:(NSString *)URLString parameters:(nullable id)parameters success:(nullable void ( ^ ) ( NSURLSessionDataTask *task , id responseObject ))success failure:(nullable void ( ^ ) ( NSURLSessionDataTask *task , NSError *error ))failure
Discussion

Creates and runs an NSURLSessionDataTask with a PATCH request.

Parameters

URLString

The URL string used to create the request URL.

parameters

The parameters to be encoded according to the client request serializer.

success

A block object to be executed when the task finishes successfully. This block has no return value and takes two arguments: the data task, and the response object created by the client response serializer.

failure

A block object to be executed when the task finishes unsuccessfully, or that finishes successfully, but encountered an error while parsing the response data. This block has no return value and takes a two arguments: the data task and the error describing the network or parsing error that occurred.

Declared In

AFHTTPSessionManager.h

POST:parameters:constructingBodyWithBlock:success:failure:

- (nullable NSURLSessionDataTask *)POST:(NSString *)URLString parameters:(nullable id)parameters constructingBodyWithBlock:(nullable void ( ^ ) ( id<AFMultipartFormData> formData ))block success:(nullable void ( ^ ) ( NSURLSessionDataTask *task , id responseObject ))success failure:(nullable void ( ^ ) ( NSURLSessionDataTask *task , NSError *error ))failure
Discussion

Creates and runs an NSURLSessionDataTask with a multipart POST request.

Parameters

URLString

The URL string used to create the request URL.

parameters

The parameters to be encoded according to the client request serializer.

block

A block that takes a single argument and appends data to the HTTP body. The block argument is an object adopting the AFMultipartFormData protocol.

success

A block object to be executed when the task finishes successfully. This block has no return value and takes two arguments: the data task, and the response object created by the client response serializer.

failure

A block object to be executed when the task finishes unsuccessfully, or that finishes successfully, but encountered an error while parsing the response data. This block has no return value and takes a two arguments: the data task and the error describing the network or parsing error that occurred.

Declared In

AFHTTPSessionManager.h

POST:parameters:success:failure:

- (nullable NSURLSessionDataTask *)POST:(NSString *)URLString parameters:(nullable id)parameters success:(nullable void ( ^ ) ( NSURLSessionDataTask *task , id responseObject ))success failure:(nullable void ( ^ ) ( NSURLSessionDataTask *task , NSError *error ))failure
Discussion

Creates and runs an NSURLSessionDataTask with a POST request.

Parameters

URLString

The URL string used to create the request URL.

parameters

The parameters to be encoded according to the client request serializer.

success

A block object to be executed when the task finishes successfully. This block has no return value and takes two arguments: the data task, and the response object created by the client response serializer.

failure

A block object to be executed when the task finishes unsuccessfully, or that finishes successfully, but encountered an error while parsing the response data. This block has no return value and takes a two arguments: the data task and the error describing the network or parsing error that occurred.

Declared In

AFHTTPSessionManager.h

PUT:parameters:success:failure:

- (nullable NSURLSessionDataTask *)PUT:(NSString *)URLString parameters:(nullable id)parameters success:(nullable void ( ^ ) ( NSURLSessionDataTask *task , id responseObject ))success failure:(nullable void ( ^ ) ( NSURLSessionDataTask *task , NSError *error ))failure
Discussion

Creates and runs an NSURLSessionDataTask with a PUT request.

Parameters

URLString

The URL string used to create the request URL.

parameters

The parameters to be encoded according to the client request serializer.

success

A block object to be executed when the task finishes successfully. This block has no return value and takes two arguments: the data task, and the response object created by the client response serializer.

failure

A block object to be executed when the task finishes unsuccessfully, or that finishes successfully, but encountered an error while parsing the response data. This block has no return value and takes a two arguments: the data task and the error describing the network or parsing error that occurred.

Declared In

AFHTTPSessionManager.h

initWithBaseURL:

- (instancetype)initWithBaseURL:(nullable NSURL *)url
Discussion

Initializes an AFHTTPSessionManager object with the specified base URL.

Parameters

url

The base URL for the HTTP client.

Return Value

The newly-initialized HTTP client

Declared In

AFHTTPSessionManager.h

initWithBaseURL:sessionConfiguration:

- (instancetype)initWithBaseURL:(nullable NSURL *)url sessionConfiguration:(nullable NSURLSessionConfiguration *)configuration
Discussion

Initializes an AFHTTPSessionManager object with the specified base URL.

This is the designated initializer.

Parameters

url

The base URL for the HTTP client.

configuration

The configuration used to create the managed session.

Return Value

The newly-initialized HTTP client

Declared In

AFHTTPSessionManager.h