FWHTTPRequestSerializer
@interface FWHTTPRequestSerializer : NSObject <FWURLRequestSerialization>
FWHTTPRequestSerializer
conforms to the FWURLRequestSerialization
& FWURLResponseSerialization
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 FWHTTPRequestSerializer
in order to ensure consistent default behavior.
-
The string encoding used to serialize parameters.
NSUTF8StringEncoding
by default.Declaration
Objective-C
@property (nonatomic) NSStringEncoding stringEncoding;
-
Whether created requests can use the device’s cellular radio (if present).
YES
by default.See
NSMutableURLRequest -setAllowsCellularAccess:Declaration
Objective-C
@property (nonatomic) BOOL allowsCellularAccess;
-
The cache policy of created requests.
NSURLRequestUseProtocolCachePolicy
by default.See
NSMutableURLRequest -setCachePolicy:Declaration
Objective-C
@property (nonatomic) NSURLRequestCachePolicy cachePolicy;
-
Whether created requests should use the default cookie handling.
YES
by default.See
NSMutableURLRequest -setHTTPShouldHandleCookies:Declaration
Objective-C
@property (nonatomic) BOOL HTTPShouldHandleCookies;
-
Whether created requests can continue transmitting data before receiving a response from an earlier transmission.
NO
by defaultSee
NSMutableURLRequest -setHTTPShouldUsePipelining:Declaration
Objective-C
@property (nonatomic) BOOL HTTPShouldUsePipelining;
-
The network service type for created requests.
NSURLNetworkServiceTypeDefault
by default.See
NSMutableURLRequest -setNetworkServiceType:Declaration
Objective-C
@property (nonatomic) NSURLRequestNetworkServiceType networkServiceType;
-
The timeout interval, in seconds, for created requests. The default timeout interval is 60 seconds.
See
NSMutableURLRequest -setTimeoutInterval:Declaration
Objective-C
@property (nonatomic) NSTimeInterval timeoutInterval;
-
Default HTTP header field values to be applied to serialized requests. By default, these include the following:
Accept-Language
with the contents ofNSLocale +preferredLanguages
User-Agent
with the contents of various bundle identifiers and OS designations
Note
To add or remove default request headers, use
setValue:forHTTPHeaderField:
.Declaration
Objective-C
@property (nonatomic, strong, readonly) NSDictionary<NSString *, NSString *> *_Nonnull HTTPRequestHeaders;
-
Creates and returns a serializer with default configuration.
Declaration
Objective-C
+ (nonnull instancetype)serializer;
-
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.Declaration
Objective-C
- (void)setValue:(nullable NSString *)value forHTTPHeaderField:(nonnull NSString *)field;
Parameters
field
The HTTP header to set a default value for
value
The value set as default for the specified header, or
nil
-
Returns the value for the HTTP headers set in the request serializer.
Declaration
Objective-C
- (nullable NSString *)valueForHTTPHeaderField:(nonnull NSString *)field;
Parameters
field
The HTTP header to retrieve the default value for
Return Value
The value set as default for the specified header, or
nil
-
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.
Declaration
Objective-C
- (void)setAuthorizationHeaderFieldWithUsername:(nonnull NSString *)username password:(nonnull NSString *)password;
Parameters
username
The HTTP basic auth username
password
The HTTP basic auth password
-
Clears any existing value for the “Authorization” HTTP header.
Declaration
Objective-C
- (void)clearAuthorizationHeader;
-
HTTP methods for which serialized requests will encode parameters as a query string.
GET
,HEAD
, andDELETE
by default.Declaration
Objective-C
@property (nonatomic, strong) NSSet<NSString *> *_Nonnull HTTPMethodsEncodingParametersInURI;
-
Set the method of query string serialization according to one of the pre-defined styles.
See
FWHTTPRequestQueryStringSerializationStyle
Declaration
Objective-C
- (void)setQueryStringSerializationWithStyle: (FWHTTPRequestQueryStringSerializationStyle)style;
Parameters
style
The serialization style.
-
Set the a custom method of query string serialization according to the specified block.
Declaration
Objective-C
- (void)setQueryStringSerializationWithBlock: (nullable NSString *_Nullable (^)( NSURLRequest *_Nonnull __strong, id _Nonnull __strong, NSError *__autoreleasing _Nullable *_Nullable))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.
-
Creates an
NSMutableURLRequest
object with the specified HTTP method and URL string.If the HTTP method is
GET
,HEAD
, orDELETE
, 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 theparameterEncoding
property, and set as the request body.Declaration
Objective-C
- (nullable NSMutableURLRequest *) requestWithMethod:(nonnull NSString *)method URLString:(nonnull NSString *)URLString parameters:(nullable id)parameters error:(NSError *__autoreleasing _Nullable *_Nullable)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 occurred while constructing the request.
Return Value
An
NSMutableURLRequest
object. -
Creates an
NSMutableURLRequest
object with the specified HTTP method and URLString, and constructs amultipart/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.2Multipart 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 anHTTPBodyStream
property, so refrain from settingHTTPBodyStream
orHTTPBody
on this request object, as it will clear out the multipart form body stream.Declaration
Objective-C
- (nonnull NSMutableURLRequest *) multipartFormRequestWithMethod:(nonnull NSString *)method URLString:(nonnull NSString *)URLString parameters: (nullable NSDictionary<NSString *, id> *)parameters constructingBodyWithBlock: (nullable void (^)(id<FWMultipartFormData> _Nonnull __strong))block error: (NSError *__autoreleasing _Nullable *_Nullable) 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
FWMultipartFormData
protocol.error
The error that occurred while constructing the request.
Return Value
An
NSMutableURLRequest
object -
Creates an
NSMutableURLRequest
by removing theHTTPBodyStream
from a request, and asynchronously writing its contents into the specified file, invoking the completion handler when finished.Note
There is a bug in
NSURLSessionTask
that causes requests to not send aContent-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 withmultipartFormRequestWithMethod:URLString:parameters:constructingBodyWithBlock:error:
, or any other request with anHTTPBodyStream
, writes the contents to the specified file and returns a copy of the original request with theHTTPBodyStream
property set tonil
. From here, the file can either be passed toFWURLSessionManager -uploadTaskWithRequest:fromFile:progress:completionHandler:
, or have its contents read into anNSData
that’s assigned to theHTTPBody
property of the request.Declaration
Objective-C
- (nonnull NSMutableURLRequest *) requestWithMultipartFormRequest:(nonnull NSURLRequest *)request writingStreamContentsToFile:(nonnull NSURL *)fileURL completionHandler: (nullable void (^)(NSError *_Nullable __strong))handler;
Parameters
request
The multipart form request. The
HTTPBodyStream
property ofrequest
must not benil
.fileURL
The file URL to write multipart form contents to.
handler
A handler block to execute.
-
Sets the “Authorization” HTTP header set in request objects made by the HTTP client to contain the access token within the OAuth credential. This overwrites any existing value for this header.
Declaration
Objective-C
- (void)setAuthorizationHeaderFieldWithCredential: (nonnull FWOAuthCredential *)credential;
Parameters
credential
The OAuth2 credential