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.
NSUTF8StringEncodingby default.Declaration
Objective-C
@property (nonatomic) NSStringEncoding stringEncoding; -
Whether created requests can use the device’s cellular radio (if present).
YESby default.See
NSMutableURLRequest -setAllowsCellularAccess:Declaration
Objective-C
@property (nonatomic) BOOL allowsCellularAccess; -
The cache policy of created requests.
NSURLRequestUseProtocolCachePolicyby default.See
NSMutableURLRequest -setCachePolicy:Declaration
Objective-C
@property (nonatomic) NSURLRequestCachePolicy cachePolicy; -
Whether created requests should use the default cookie handling.
YESby 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.
NOby defaultSee
NSMutableURLRequest -setHTTPShouldUsePipelining:Declaration
Objective-C
@property (nonatomic) BOOL HTTPShouldUsePipelining; -
The network service type for created requests.
NSURLNetworkServiceTypeDefaultby 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-Languagewith the contents ofNSLocale +preferredLanguagesUser-Agentwith 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
fieldThe HTTP header to set a default value for
valueThe 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
fieldThe 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
usernameThe HTTP basic auth username
passwordThe 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, andDELETEby 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
styleThe 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
blockA 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
NSMutableURLRequestobject 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 theparameterEncodingproperty, 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
methodThe HTTP method for the request, such as
GET,POST,PUT, orDELETE. This parameter must not benil.URLStringThe URL string used to create the request URL.
parametersThe parameters to be either set as a query string for
GETrequests, or the request HTTP body.errorThe error that occurred while constructing the request.
Return Value
An
NSMutableURLRequestobject. -
Creates an
NSMutableURLRequestobject with the specified HTTP method and URLString, and constructs amultipart/form-dataHTTP 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
NSMutableURLRequestobject has anHTTPBodyStreamproperty, so refrain from settingHTTPBodyStreamorHTTPBodyon 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
methodThe HTTP method for the request. This parameter must not be
GETorHEAD, ornil.URLStringThe URL string used to create the request URL.
parametersThe parameters to be encoded and set in the request HTTP body.
blockA block that takes a single argument and appends data to the HTTP body. The block argument is an object adopting the
FWMultipartFormDataprotocol.errorThe error that occurred while constructing the request.
Return Value
An
NSMutableURLRequestobject -
Creates an
NSMutableURLRequestby removing theHTTPBodyStreamfrom a request, and asynchronously writing its contents into the specified file, invoking the completion handler when finished.Note
There is a bug in
NSURLSessionTaskthat causes requests to not send aContent-Lengthheader 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 theHTTPBodyStreamproperty set tonil. From here, the file can either be passed toFWURLSessionManager -uploadTaskWithRequest:fromFile:progress:completionHandler:, or have its contents read into anNSDatathat’s assigned to theHTTPBodyproperty of the request.Declaration
Objective-C
- (nonnull NSMutableURLRequest *) requestWithMultipartFormRequest:(nonnull NSURLRequest *)request writingStreamContentsToFile:(nonnull NSURL *)fileURL completionHandler: (nullable void (^)(NSError *_Nullable __strong))handler;Parameters
requestThe multipart form request. The
HTTPBodyStreamproperty ofrequestmust not benil.fileURLThe file URL to write multipart form contents to.
handlerA 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
credentialThe OAuth2 credential
FWHTTPRequestSerializer Class Reference