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


class NSMutableURLRequest : NSURLRequest


NSMutableURLRequest is a subclass of NSURLRequest that enables you to mutate a single request object for a series of URL load requests instead of creating an immutable NSURLRequest object for each load.

NSMutableURLRequest, like 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 URLSession, NSURLConnection, and NSURLDownload classes make a deep copy of each NSMutableURLRequest object passed to their initializers and task creation methods.

Reserved HTTP Headers

The URL Loading System is designed to handle various aspects of the HTTP protocol for you. As a result, you should not modify the following headers using the addValue(_:forHTTPHeaderField:) or setValue(_:forHTTPHeaderField:) methods:

  • Authorization

  • Connection

  • Host

  • Proxy-Authenticate

  • Proxy-Authorization

  • WWW-Authenticate


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.

Accessing Header Fields

var allHTTPHeaderFields: [String : String]?

The request’s header fields.

func addValue(String, forHTTPHeaderField: String)

Adds an HTTP header to the request’s HTTP header dictionary.

func setValue(String?, forHTTPHeaderField: String)

Sets 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 request should use the default cookie handling for the request.

var httpShouldUsePipelining: Bool

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

var allowsCellularAccess: Bool

A Boolean value that indicates whether a connection can use the device’s cellular network (if present).

Accessing the Service Type

var networkServiceType: NSURLRequest.NetworkServiceType

The network service type of the connection.

enum NSURLRequest.NetworkServiceType

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

Working with Hotspots

func bind(to: NEHotspotHelperCommand)

Binds a URL request to the network interface associated with the hotspot helper command instance.


Inherits From

Conforms To

See Also

Using Reference Types

class NSURLRequest

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

typealias URLRequest.ReferenceType

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