A mutable object that represents a URL load request that bridges to URLRequest; use NSMutableURLRequest when you need reference semantics or other Foundation-specific behavior.


NSMutableURLRequest is a subclass of NSURLRequest provided to aid developers who may find it more convenient 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 adding categories 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


Setting Request Properties

var cachePolicy: NSURLRequest.CachePolicy

The cache policy of the receiver.

var mainDocumentURL: URL?

The main document URL for the receiver.

var networkServiceType: NSURLRequest.NetworkServiceType

The network service type of the connection.

var timeoutInterval: TimeInterval

The receiver’s timeout interval, in seconds.

var url: URL?

The URL of the receiver

var allowsCellularAccess: Bool

A Boolean value that indicates whether the connection can use the device’s cellular radio (if present). true by default

Setting HTTP Specific Properties

func addValue(String, forHTTPHeaderField: String)

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

var allHTTPHeaderFields: [String : String]?

The receiver's header fields.

var httpBody: Data?

The request body of the receiver.

var httpBodyStream: InputStream?

The request body as an input stream.

var httpMethod: String

The receiver’s HTTP request method.

var httpShouldHandleCookies: Bool

A boolean value that indicates whether the receiver 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.

func setValue(String?, forHTTPHeaderField: String)

Sets the specified HTTP header field.


Inherits From

Conforms To

See Also

Requests and Responses

struct URLRequest

A URL load request that is independent of protocol or URL scheme.

class URLResponse

The metadata associated with the response to a URL load request, independent of protocol and URL scheme.

class HTTPURLResponse

The metadata associated with the response to an HTTP protocol URL load request.