Inherits from NSObject
Conforms to AFURLRequestSerialization
Declared in AFURLRequestSerialization.h

Overview

AFHTTPSerializer 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 AFHTTPSerializer in order to ensure consistent default behavior.

Tasks

Other Methods

Configuring HTTP Request Headers

  •   HTTPRequestHeaders

    Default HTTP header field values to be applied to serialized requests.

    property
  • + serializer

    Creates and returns a serializer with default configuration.

  • – setValue:forHTTPHeaderField:

    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.

  • – setAuthorizationHeaderFieldWithUsername:password:

    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.

  • – setAuthorizationHeaderFieldWithToken:

    Sets the “Authorization” HTTP header set in request objects made by the HTTP client to a token-based authentication value, such as an OAuth access token. This overwrites any existing value for this header.

  • – clearAuthorizationHeader

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

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.

Declared In

AFURLRequestSerialization.h

stringEncoding

@property (nonatomic, assign) NSStringEncoding stringEncoding
Discussion

The string encoding used to serialize parameters.

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:

- (NSMutableURLRequest *)multipartFormRequestWithMethod:(NSString *)method URLString:(NSString *)URLString parameters:(NSDictionary *)parameters constructingBodyWithBlock:(void ( ^ ) ( id<AFMultipartFormData> formData ))block
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.

Return Value

An NSMutableURLRequest object

Declared In

AFURLRequestSerialization.h

requestWithMethod:URLString:parameters:

- (NSMutableURLRequest *)requestWithMethod:(NSString *)method URLString:(NSString *)URLString parameters:(NSDictionary *)parameters
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.

Return Value

An NSMutableURLRequest object.

Declared In

AFURLRequestSerialization.h

setAuthorizationHeaderFieldWithToken:

- (void)setAuthorizationHeaderFieldWithToken:(NSString *)token
Discussion

Sets the “Authorization” HTTP header set in request objects made by the HTTP client to a token-based authentication value, such as an OAuth access token. This overwrites any existing value for this header.

Parameters

token

The authentication 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 , NSDictionary *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