A representation of a URL load request that you use when you need reference semantics or other Foundation-specific behavior.


class NSURLRequest : NSObject


NSURLRequest encapsulates two basic data elements of a load request: the URL to load, and the policy to use when consulting the URL content cache made available by the implementation.

NSURLRequest is designed to be extended to support additional protocols by creating extensions that provide accessor methods for your own protocol-specific properties. Those methods can get and set the actual values by calling the URLProtocol methods property(forKey:in:) and setProperty(_:forKey:in:).

The mutable subclass of NSURLRequest is NSMutableURLRequest.


Creating Requests

init(url: URL)

Creates a URL request for a specified URL.

init(url: URL, cachePolicy: NSURLRequest.CachePolicy, timeoutInterval: TimeInterval)

Creates a URL request with the specified URL, cache policy, and timeout values.

Working with a Cache Policy

var cachePolicy: NSURLRequest.CachePolicy

The request’s cache policy.

enum NSURLRequest.CachePolicy

The constants used to specify interaction with the cached responses.

Accessing Request Components

var httpMethod: String?

The HTTP request method.

var url: URL?

The URL being requested.

var httpBody: Data?

The request body.

var httpBodyStream: InputStream?

The request body as an input stream.

var mainDocumentURL: URL?

The main document URL associated with the request.

Getting Header Fields

var allHTTPHeaderFields: [String : String]?

All of the request’s HTTP header fields.

func value(forHTTPHeaderField: String) -> String?

Returns the value of the specified HTTP header field.

Controlling Request Behavior

var timeoutInterval: TimeInterval

The request’s timeout interval, in seconds.

var httpShouldHandleCookies: Bool

A Boolean value that indicates whether the default cookie handling will be used for this request.

var httpShouldUsePipelining: Bool

A Boolean value that indicates whether the request should continue transmitting data before receiving a response from an earlier transmission.

var allowsCellularAccess: Bool

A Boolean value that indicates whether the request is allowed to use the cellular radio (if present).

Accessing the Service Type

var networkServiceType: NSURLRequest.NetworkServiceType

The network service type of the request.

enum NSURLRequest.NetworkServiceType

The constants used to specify the network service type of a request.

Supporting Secure Coding

class var supportsSecureCoding: Bool

A Boolean value indicating whether the NSURLRequest implements the NSSecureCoding protocol.

See Also

Using Reference Types

class NSMutableURLRequest

A mutable URL load request that bridges to NSURLRequest; use NSMutableURLRequest when you need reference semantics or other Foundation-specific behavior

typealias URLRequest.ReferenceType

An alias for this value type's equivalent reference type.