LCHTTPRequestSerializer
Objective-C
@interface LCHTTPRequestSerializer : NSObject <LCURLRequestSerialization>
Swift
class LCHTTPRequestSerializer : NSObject, LCURLRequestSerialization
LCHTTPRequestSerializer conforms to the LCURLRequestSerialization & LCURLResponseSerialization 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 LCHTTPRequestSerializer in order to ensure consistent default behavior.
-
The string encoding used to serialize parameters.
NSUTF8StringEncodingby default.Declaration
Objective-C
@property (nonatomic) NSStringEncoding stringEncoding;Swift
var stringEncoding: UInt { get set } -
Whether created requests can use the device’s cellular radio (if present).
YESby default.See
NSMutableURLRequest -setAllowsCellularAccess:Declaration
Objective-C
@property (nonatomic) BOOL allowsCellularAccess;Swift
var allowsCellularAccess: Bool { get set } -
The cache policy of created requests.
NSURLRequestUseProtocolCachePolicyby default.See
NSMutableURLRequest -setCachePolicy:Declaration
Objective-C
@property (nonatomic) NSURLRequestCachePolicy cachePolicy;Swift
var cachePolicy: NSURLRequest.CachePolicy { get set } -
Whether created requests should use the default cookie handling.
YESby default.See
NSMutableURLRequest -setHTTPShouldHandleCookies:Declaration
Objective-C
@property (nonatomic) BOOL HTTPShouldHandleCookies;Swift
var httpShouldHandleCookies: Bool { get set } -
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;Swift
var httpShouldUsePipelining: Bool { get set } -
The network service type for created requests.
NSURLNetworkServiceTypeDefaultby default.See
NSMutableURLRequest -setNetworkServiceType:Declaration
Objective-C
@property (nonatomic) NSURLRequestNetworkServiceType networkServiceType;Swift
var networkServiceType: NSURLRequest.NetworkServiceType { get set } -
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;Swift
var timeoutInterval: TimeInterval { get set }
-
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
@discussion To add or remove default request headers, use
setValue:forHTTPHeaderField:.Declaration
Objective-C
@property (nonatomic, strong, readonly) NSDictionary<NSString *, NSString *> *_Nonnull HTTPRequestHeaders;Swift
var httpRequestHeaders: [String : String] { get } -
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;Swift
func setValue(_ value: String?, forHTTPHeaderField field: String)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;Swift
func value(forHTTPHeaderField field: String) -> String?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;Swift
func setAuthorizationHeaderFieldWithUsername(_ username: String, password: String)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;Swift
func 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;Swift
var httpMethodsEncodingParametersInURI: Set<String> { get set } -
Set the method of query string serialization according to one of the pre-defined styles.
See
LCHTTPRequestQueryStringSerializationStyle
Declaration
Objective-C
- (void)setQueryStringSerializationWithStyle: (LCHTTPRequestQueryStringSerializationStyle)style;Swift
func setQueryStringSerializationWith(_ style: LCHTTPRequestQueryStringSerializationStyle)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 *_Nonnull (^)(NSURLRequest *_Nonnull, id _Nonnull, NSError *_Nullable *_Nullable))block;Swift
func setQueryStringSerializationWith(_ block: ((URLRequest, Any, NSErrorPointer) -> String)?)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
- (nonnull NSMutableURLRequest *) requestWithMethod:(nonnull NSString *)method URLString:(nonnull NSString *)URLString parameters:(nullable id)parameters error:(NSError *_Nullable *_Nullable)error;Swift
func request(withMethod method: String, urlString URLString: String, parameters: Any?, error: NSErrorPointer) -> NSMutableURLRequestParameters
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<LCMultipartFormData> _Nonnull))block error:(NSError *_Nullable *_Nullable)error;Swift
func multipartFormRequest(withMethod method: String, urlString URLString: String, parameters: [String : Any]?, constructingBodyWith block: ((LCMultipartFormData) -> Void)?, error: NSErrorPointer) -> NSMutableURLRequestParameters
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
LCMultipartFormDataprotocol.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.@discussion 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 toLCURLSessionManager -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))handler;Swift
func request(withMultipartForm request: URLRequest, writingStreamContentsToFile fileURL: URL, completionHandler handler: ((Error?) -> Void)? = nil) -> NSMutableURLRequestParameters
requestThe multipart form request. The
HTTPBodyStreamproperty ofrequestmust not benil.fileURLThe file URL to write multipart form contents to.
handlerA handler block to execute.
View on GitHub
Install in Dash
LCHTTPRequestSerializer Class Reference