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
-
stringEncodingproperty -
allowsCellularAccessproperty -
cachePolicyproperty -
HTTPShouldHandleCookiesproperty -
HTTPShouldUsePipeliningproperty -
networkServiceTypeproperty -
timeoutIntervalproperty
Configuring HTTP Request Headers
-
HTTPRequestHeadersproperty -
+ serializer -
– setValue:forHTTPHeaderField: -
– setAuthorizationHeaderFieldWithUsername:password: -
– setAuthorizationHeaderFieldWithToken: -
– clearAuthorizationHeader
Configuring Query String Parameter Serialization
-
HTTPMethodsEncodingParametersInURIproperty -
– 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 *HTTPMethodsEncodingParametersInURIDeclared In
AFURLRequestSerialization.hHTTPRequestHeaders
Default HTTP header field values to be applied to serialized requests.
@property (readonly, nonatomic, strong) NSDictionary *HTTPRequestHeadersDeclared In
AFURLRequestSerialization.hHTTPShouldHandleCookies
Whether created requests should use the default cookie handling. YES by default.
@property (nonatomic, assign) BOOL HTTPShouldHandleCookiesSee Also
NSMutableURLRequest @property HTTPShouldHandleCookies
Declared In
AFURLRequestSerialization.hHTTPShouldUsePipelining
Whether created requests can continue transmitting data before receiving a response from an earlier transmission. NO by default
@property (nonatomic, assign) BOOL HTTPShouldUsePipeliningSee Also
NSMutableURLRequest @property HTTPShouldUsePipelining
Declared In
AFURLRequestSerialization.hallowsCellularAccess
Whether created requests can use the device’s cellular radio (if present). YES by default.
@property (nonatomic, assign) BOOL allowsCellularAccessSee Also
NSMutableURLRequest @property allowsCellularAccess
Declared In
AFURLRequestSerialization.hcachePolicy
The cache policy of created requests. NSURLRequestUseProtocolCachePolicy by default.
@property (nonatomic, assign) NSURLRequestCachePolicy cachePolicySee Also
NSMutableURLRequest @property cachePolicy
Declared In
AFURLRequestSerialization.hnetworkServiceType
The network service type for created requests. NSURLNetworkServiceTypeDefault by default.
@property (nonatomic, assign) NSURLRequestNetworkServiceType networkServiceTypeSee Also
NSMutableURLRequest @property networkServiceType
Declared In
AFURLRequestSerialization.hstringEncoding
The string encoding used to serialize parameters. NSUTF8StringEncoding by default.
@property (nonatomic, assign) NSStringEncoding stringEncodingDeclared In
AFURLRequestSerialization.htimeoutInterval
The timeout interval, in seconds, for created requests. The default timeout interval is 60 seconds.
@property (nonatomic, assign) NSTimeInterval timeoutIntervalSee Also
NSMutableURLRequest @property timeoutInterval
Declared In
AFURLRequestSerialization.hInstance Methods
clearAuthorizationHeader
Clears any existing value for the “Authorization” HTTP header.
- (void)clearAuthorizationHeaderDeclared In
AFURLRequestSerialization.hmultipartFormRequestWithMethod: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 ))blockDeclared In
AFURLRequestSerialization.hmultipartFormRequestWithMethod: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 *)errorParameters
- method
The HTTP method for the request. This parameter must not be
GETorHEAD, 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
AFMultipartFormDataprotocol.
- 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.hrequestBySerializingRequest:withParameters:error:
- (NSURLRequest *)requestBySerializingRequest:(NSURLRequest *)request withParameters:(id)parameters error:(NSError *__autoreleasing *)errorDeclared In
AFURLRequestSerialization.hrequestWithMethod: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)parametersDeclared In
AFURLRequestSerialization.hrequestWithMethod: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 *)errorParameters
- 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
GETrequests, 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.hrequestWithMultipartFormRequest: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 ))handlerParameters
- 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.hsetAuthorizationHeaderFieldWithToken:
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 *)tokenParameters
- token
The authentication token
Declared In
AFURLRequestSerialization.hsetAuthorizationHeaderFieldWithUsername: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 *)passwordParameters
- username
The HTTP basic auth username
- password
The HTTP basic auth password
Declared In
AFURLRequestSerialization.hsetQueryStringSerializationWithBlock:
Set the a custom method of query string serialization according to the specified block.
- (void)setQueryStringSerializationWithBlock:(NSString *( ^ ) ( NSURLRequest *request , NSDictionary *parameters , NSError *__autoreleasing *error ))blockParameters
- 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.hsetQueryStringSerializationWithStyle:
Set the method of query string serialization according to one of the pre-defined styles.
- (void)setQueryStringSerializationWithStyle:(AFHTTPRequestQueryStringSerializationStyle)styleParameters
- style
The serialization style.
Declared In
AFURLRequestSerialization.hsetValue: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 *)fieldParameters
- 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