Inherits from NSObject
Conforms to AFURLRequestSerialization
Declared in AFURLRequestSerialization.h

Overview

AFHTTPRequestSerializer conforms to the AFURLRequestSerialization & AFURLResponseSerialization protocols, offering a concrete base implementation of query string / URL form-encoded parameter serialization and default request headers, as well as response status code and content type validation.

Any request or response serializer dealing with HTTP is encouraged to subclass AFHTTPRequestSerializer in order to ensure consistent default behavior.

Tasks

Other Methods

  •   stringEncoding

    The string encoding used to serialize parameters. NSUTF8StringEncoding by default.

    property
  •   allowsCellularAccess

    Whether created requests can use the device’s cellular radio (if present). YES by default.

    property
  •   cachePolicy

    The cache policy of created requests. NSURLRequestUseProtocolCachePolicy by default.

    property
  •   HTTPShouldHandleCookies

    Whether created requests should use the default cookie handling. YES by default.

    property
  •   HTTPShouldUsePipelining

    Whether created requests can continue transmitting data before receiving a response from an earlier transmission. NO by default

    property
  •   networkServiceType

    The network service type for created requests. NSURLNetworkServiceTypeDefault by default.

    property
  •   timeoutInterval

    The timeout interval, in seconds, for created requests. The default timeout interval is 60 seconds.

    property

Configuring HTTP Request Headers

Configuring Query String Parameter Serialization

Creating Request Objects

Properties

HTTPMethodsEncodingParametersInURI

@property (nonatomic, strong) NSSet *HTTPMethodsEncodingParametersInURI
Discussion

HTTP methods for which serialized requests will encode parameters as a query string. GET, HEAD, and DELETE by default.

Declared In

AFURLRequestSerialization.h

HTTPRequestHeaders

@property (readonly, nonatomic, strong) NSDictionary *HTTPRequestHeaders
Discussion

Default HTTP header field values to be applied to serialized requests. By default, these include the following:

  • Accept-Language with the contents of NSLocale +preferredLanguages
  • User-Agent with the contents of various bundle identifiers and OS designations

To add or remove default request headers, use setValue:forHTTPHeaderField:.

Declared In

AFURLRequestSerialization.h

HTTPShouldHandleCookies

@property (nonatomic, assign) BOOL HTTPShouldHandleCookies
Discussion

Whether created requests should use the default cookie handling. YES by default.

See Also

Declared In

AFURLRequestSerialization.h

HTTPShouldUsePipelining

@property (nonatomic, assign) BOOL HTTPShouldUsePipelining
Discussion

Whether created requests can continue transmitting data before receiving a response from an earlier transmission. NO by default

See Also

Declared In

AFURLRequestSerialization.h

allowsCellularAccess

@property (nonatomic, assign) BOOL allowsCellularAccess
Discussion

Whether created requests can use the device’s cellular radio (if present). YES by default.

See Also

Declared In

AFURLRequestSerialization.h

cachePolicy

@property (nonatomic, assign) NSURLRequestCachePolicy cachePolicy
Discussion

The cache policy of created requests. NSURLRequestUseProtocolCachePolicy by default.

See Also

Declared In

AFURLRequestSerialization.h

networkServiceType

@property (nonatomic, assign) NSURLRequestNetworkServiceType networkServiceType
Discussion

The network service type for created requests. NSURLNetworkServiceTypeDefault by default.

See Also

Declared In

AFURLRequestSerialization.h

stringEncoding

@property (nonatomic, assign) NSStringEncoding stringEncoding
Discussion

The string encoding used to serialize parameters. NSUTF8StringEncoding by default.

Declared In

AFURLRequestSerialization.h

timeoutInterval

@property (nonatomic, assign) NSTimeInterval timeoutInterval
Discussion

The timeout interval, in seconds, for created requests. The default timeout interval is 60 seconds.

See Also

Declared In

AFURLRequestSerialization.h

Class Methods

serializer

+ (instancetype)serializer
Discussion

Creates and returns a serializer with default configuration.

Declared In

AFURLRequestSerialization.h

Instance Methods

clearAuthorizationHeader

- (void)clearAuthorizationHeader
Discussion

Clears any existing value for the “Authorization” HTTP header.

Declared In

AFURLRequestSerialization.h

multipartFormRequestWithMethod:URLString:parameters:constructingBodyWithBlock:

This method has been deprecated. Use multipartFormRequestWithMethod:URLString:parameters:constructingBodyWithBlock:error: instead. (Deprecated: This method has been deprecated. Use -multipartFormRequestWithMethod:URLString:parameters:constructingBodyWithBlock:error: instead.)

- (NSMutableURLRequest *)multipartFormRequestWithMethod:(NSString *)method URLString:(NSString *)URLString parameters:(NSDictionary *)parameters constructingBodyWithBlock:(void ( ^ ) ( id<AFMultipartFormData> formData ))block

Declared In

AFURLRequestSerialization.h

multipartFormRequestWithMethod:URLString:parameters:constructingBodyWithBlock:error:

- (NSMutableURLRequest *)multipartFormRequestWithMethod:(NSString *)method URLString:(NSString *)URLString parameters:(NSDictionary *)parameters constructingBodyWithBlock:(void ( ^ ) ( id<AFMultipartFormData> formData ))block error:(NSError *__autoreleasing *)error
Discussion

Creates an NSMutableURLRequest object with the specified HTTP method and URLString, and constructs a multipart/form-data HTTP body, using the specified parameters and multipart form data block. See http://www.w3.org/TR/html4/interact/forms.html#h-17.13.4.2

Multipart form requests are automatically streamed, reading files directly from disk along with in-memory data in a single HTTP body. The resulting NSMutableURLRequest object has an HTTPBodyStream property, so refrain from setting HTTPBodyStream or HTTPBody on this request object, as it will clear out the multipart form body stream.

Parameters

method

The HTTP method for the request. This parameter must not be GET or HEAD, or nil.

URLString

The URL string used to create the request URL.

parameters

The parameters to be encoded and set in the request HTTP body.

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.

error

The error that occured while constructing the request.

Return Value

An NSMutableURLRequest object

Declared In

AFURLRequestSerialization.h

requestWithMethod:URLString:parameters:

This method has been deprecated. Use requestWithMethod:URLString:parameters:error: instead. (Deprecated: This method has been deprecated. Use -requestWithMethod:URLString:parameters:error: instead.)

- (NSMutableURLRequest *)requestWithMethod:(NSString *)method URLString:(NSString *)URLString parameters:(id)parameters

Declared In

AFURLRequestSerialization.h

requestWithMethod:URLString:parameters:error:

- (NSMutableURLRequest *)requestWithMethod:(NSString *)method URLString:(NSString *)URLString parameters:(id)parameters error:(NSError *__autoreleasing *)error
Discussion

Creates an NSMutableURLRequest object with the specified HTTP method and URL string.

If the HTTP method is GET, HEAD, or DELETE, the parameters will be used to construct a url-encoded query string that is appended to the request’s URL. Otherwise, the parameters will be encoded according to the value of the parameterEncoding property, and set as the request body.

Parameters

method

The HTTP method for the request, such as GET, POST, PUT, or DELETE. This parameter must not be nil.

URLString

The URL string used to create the request URL.

parameters

The parameters to be either set as a query string for GET requests, or the request HTTP body.

error

The error that occured while constructing the request.

Return Value

An NSMutableURLRequest object.

Declared In

AFURLRequestSerialization.h

requestWithMultipartFormRequest:writingStreamContentsToFile:completionHandler:

- (NSMutableURLRequest *)requestWithMultipartFormRequest:(NSURLRequest *)request writingStreamContentsToFile:(NSURL *)fileURL completionHandler:(void ( ^ ) ( NSError *error ))handler
Discussion

Creates an NSMutableURLRequest by removing the HTTPBodyStream from a request, and asynchronously writing its contents into the specified file, invoking the completion handler when finished.

There is a bug in NSURLSessionTask that causes requests to not send a Content-Length header when streaming contents from an HTTP body, which is notably problematic when interacting with the Amazon S3 webservice. As a workaround, this method takes a request constructed with multipartFormRequestWithMethod:URLString:parameters:constructingBodyWithBlock:error:, or any other request with an HTTPBodyStream, writes the contents to the specified file and returns a copy of the original request with the HTTPBodyStream property set to nil. From here, the file can either be passed to AFURLSessionManager -uploadTaskWithRequest:fromFile:progress:completionHandler:, or have its contents read into an NSData that’s assigned to the HTTPBody property of the request.

Parameters

request

The multipart form request. The HTTPBodyStream property of request must not be nil.

fileURL

The file URL to write multipart form contents to.

handler

A handler block to execute.

Declared In

AFURLRequestSerialization.h

setAuthorizationHeaderFieldWithToken:

This method has been deprecated. Use setValue:forHTTPHeaderField: instead. (Deprecated: This method has been deprecated. Use -setValue:forHTTPHeaderField: instead.)

- (void)setAuthorizationHeaderFieldWithToken:(NSString *)token

Declared In

AFURLRequestSerialization.h

setAuthorizationHeaderFieldWithUsername:password:

- (void)setAuthorizationHeaderFieldWithUsername:(NSString *)username password:(NSString *)password
Discussion

Sets the “Authorization” HTTP header set in request objects made by the HTTP client to a basic authentication value with Base64-encoded username and password. This overwrites any existing value for this header.

Parameters

username

The HTTP basic auth username

password

The HTTP basic auth password

Declared In

AFURLRequestSerialization.h

setQueryStringSerializationWithBlock:

- (void)setQueryStringSerializationWithBlock:(NSString *( ^ ) ( NSURLRequest *request , id parameters , NSError *__autoreleasing *error ))block
Discussion

Set the a custom method of query string serialization according to the specified block.

Parameters

block

A block that defines a process of encoding parameters into a query string. This block returns the query string and takes three arguments: the request, the parameters to encode, and the error that occurred when attempting to encode parameters for the given request.

Declared In

AFURLRequestSerialization.h

setQueryStringSerializationWithStyle:

- (void)setQueryStringSerializationWithStyle:(AFHTTPRequestQueryStringSerializationStyle)style
Discussion

Set the method of query string serialization according to one of the pre-defined styles.

Parameters

style

The serialization style.

Declared In

AFURLRequestSerialization.h

setValue:forHTTPHeaderField:

- (void)setValue:(NSString *)value forHTTPHeaderField:(NSString *)field
Discussion

Sets the value for the HTTP headers set in request objects made by the HTTP client. If nil, removes the existing value for that header.

Parameters

value

The value set as default for the specified header, or nil

field

The HTTP header to set a default value for

Declared In

AFURLRequestSerialization.h

valueForHTTPHeaderField:

- (NSString *)valueForHTTPHeaderField:(NSString *)field
Discussion

Returns the value for the HTTP headers set in the request serializer.

Parameters

field

The HTTP header to retrieve the default value for

Return Value

The value set as default for the specified header, or nil

Declared In

AFURLRequestSerialization.h