AFHTTPRequestSerializer Class Reference
Inherits from | NSObject |
Conforms to | AFURLRequestSerialization |
Declared in | AFURLRequestSerialization.h AFURLRequestSerialization.m |
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
property -
allowsCellularAccess
property -
cachePolicy
property -
HTTPShouldHandleCookies
property -
HTTPShouldUsePipelining
property -
networkServiceType
property -
timeoutInterval
property
Configuring HTTP Request Headers
-
HTTPRequestHeaders
property -
+ serializer
-
– setValue:forHTTPHeaderField:
-
– setAuthorizationHeaderFieldWithUsername:password:
-
– setAuthorizationHeaderFieldWithToken:
-
– clearAuthorizationHeader
Configuring Query String Parameter Serialization
-
HTTPMethodsEncodingParametersInURI
property -
– setQueryStringSerializationWithStyle:
-
– setQueryStringSerializationWithBlock:
Creating Request Objects
-
– requestWithMethod:URLString:parameters:
-
– requestWithMethod:URLString:parameters:error:
-
– multipartFormRequestWithMethod:URLString:parameters:constructingBodyWithBlock:
-
– multipartFormRequestWithMethod:URLString:parameters:constructingBodyWithBlock:error:
-
– requestWithMultipartFormRequest:writingStreamContentsToFile:completionHandler:
Other Methods
Properties
HTTPMethodsEncodingParametersInURI
HTTP methods for which serialized requests will encode parameters as a query string. GET
, HEAD
, and DELETE
by default.
@property (nonatomic, strong) NSSet *HTTPMethodsEncodingParametersInURI
Declared In
AFURLRequestSerialization.h
HTTPRequestHeaders
Default HTTP header field values to be applied to serialized requests.
@property (readonly, nonatomic, strong) NSDictionary *HTTPRequestHeaders
Declared In
AFURLRequestSerialization.h
HTTPShouldHandleCookies
Whether created requests should use the default cookie handling. YES
by default.
@property (nonatomic, assign) BOOL HTTPShouldHandleCookies
See Also
NSMutableURLRequest @property HTTPShouldHandleCookies
Declared In
AFURLRequestSerialization.h
HTTPShouldUsePipelining
Whether created requests can continue transmitting data before receiving a response from an earlier transmission. NO
by default
@property (nonatomic, assign) BOOL HTTPShouldUsePipelining
See Also
NSMutableURLRequest @property HTTPShouldUsePipelining
Declared In
AFURLRequestSerialization.h
allowsCellularAccess
Whether created requests can use the device’s cellular radio (if present). YES
by default.
@property (nonatomic, assign) BOOL allowsCellularAccess
See Also
NSMutableURLRequest @property allowsCellularAccess
Declared In
AFURLRequestSerialization.h
cachePolicy
The cache policy of created requests. NSURLRequestUseProtocolCachePolicy
by default.
@property (nonatomic, assign) NSURLRequestCachePolicy cachePolicy
See Also
NSMutableURLRequest @property cachePolicy
Declared In
AFURLRequestSerialization.h
networkServiceType
The network service type for created requests. NSURLNetworkServiceTypeDefault
by default.
@property (nonatomic, assign) NSURLRequestNetworkServiceType networkServiceType
See Also
NSMutableURLRequest @property networkServiceType
Declared In
AFURLRequestSerialization.h
stringEncoding
The string encoding used to serialize parameters. NSUTF8StringEncoding
by default.
@property (nonatomic, assign) NSStringEncoding stringEncoding
Declared In
AFURLRequestSerialization.h
timeoutInterval
The timeout interval, in seconds, for created requests. The default timeout interval is 60 seconds.
@property (nonatomic, assign) NSTimeInterval timeoutInterval
See Also
NSMutableURLRequest @property timeoutInterval
Declared In
AFURLRequestSerialization.h
Instance Methods
clearAuthorizationHeader
Clears any existing value for the “Authorization” HTTP header.
- (void)clearAuthorizationHeader
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:
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
- (NSMutableURLRequest *)multipartFormRequestWithMethod:(NSString *)method URLString:(NSString *)URLString parameters:(NSDictionary *)parameters constructingBodyWithBlock:(void ( ^ ) ( id<AFMultipartFormData> formData ))block error:(NSError *__autoreleasing *)error
Parameters
- method
The HTTP method for the request. This parameter must not be
GET
orHEAD
, ornil
.
- 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
Discussion
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.
Declared In
AFURLRequestSerialization.h
requestBySerializingRequest:withParameters:error:
- (NSURLRequest *)requestBySerializingRequest:(NSURLRequest *)request withParameters:(id)parameters error:(NSError *__autoreleasing *)error
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:
Creates an NSMutableURLRequest
object with the specified HTTP method and URL string.
- (NSMutableURLRequest *)requestWithMethod:(NSString *)method URLString:(NSString *)URLString parameters:(id)parameters error:(NSError *__autoreleasing *)error
Parameters
- method
The HTTP method for the request, such as
GET
,POST
,PUT
, orDELETE
. This parameter must not benil
.
- 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.
Discussion
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.
Declared In
AFURLRequestSerialization.h
requestWithMultipartFormRequest:writingStreamContentsToFile:completionHandler:
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.
- (NSMutableURLRequest *)requestWithMultipartFormRequest:(NSURLRequest *)request writingStreamContentsToFile:(NSURL *)fileURL completionHandler:(void ( ^ ) ( NSError *error ))handler
Parameters
- request
The multipart form request.
- fileURL
The file URL to write multipart form contents to.
- handler
A handler block to execute.
Discussion
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.
Declared In
AFURLRequestSerialization.h
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.
- (void)setAuthorizationHeaderFieldWithToken:(NSString *)token
Parameters
- token
The authentication token
Declared In
AFURLRequestSerialization.h
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.
- (void)setAuthorizationHeaderFieldWithUsername:(NSString *)username password:(NSString *)password
Parameters
- username
The HTTP basic auth username
- password
The HTTP basic auth password
Declared In
AFURLRequestSerialization.h
setQueryStringSerializationWithBlock:
Set the a custom method of query string serialization according to the specified block.
- (void)setQueryStringSerializationWithBlock:(NSString *( ^ ) ( NSURLRequest *request , NSDictionary *parameters , NSError *__autoreleasing *error ))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:
Set the method of query string serialization according to one of the pre-defined styles.
- (void)setQueryStringSerializationWithStyle:(AFHTTPRequestQueryStringSerializationStyle)style
Parameters
- style
The serialization style.
Declared In
AFURLRequestSerialization.h
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.
- (void)setValue:(NSString *)value forHTTPHeaderField:(NSString *)field
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