Class

NSURLRequest

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

Declaration

@interface NSURLRequest : NSObject

Overview

NSURLRequest encapsulates two essential properties of a load request: the URL to load and the policies used to load it. In addition, for HTTP and HTTPS requests, URLRequest includes the HTTP method (GET, POST, and so on) and the HTTP headers. Finally, custom protocols can support custom properties as explained in Custom Protocol Properties.

NSURLRequest only represents information about the request. Use other classes, such as NSURLSession, to send the request to a server. See Fetching Website Data into Memory and Uploading Data to a Website for an introduction to these techniques.

The mutable subclass of NSURLRequest is NSMutableURLRequest.

Reserved HTTP Headers

The URL Loading System handles various aspects of the HTTP protocol for you (HTTP 1.1 persistent connections, proxies, authentication, and so on). As part of this support, the URL Loading System takes responsibility for certain HTTP headers:

  • Content-Length

  • Authorization

  • Connection

  • Host

  • Proxy-Authenticate

  • Proxy-Authorization

  • WWW-Authenticate

If you set a value for one of these reserved headers, the system may ignore the value you set, or overwrite it with its own value, or simply not send it. Moreover, the exact behavior may change over time. To avoid confusing problems like this, do not set these headers directly.

The URL Loading System sets the Content-Length header based on whether the request body has a known length:

  • If so, it uses the identity transfer encoding and sets the Content-Length header to that known length. You see this when you set the request body to a data object.

  • If not, it uses the chunked transfer encoding and omits the Content-Length header. You see this when you set the request body to a stream.

Custom Protocol Properties

If you implement a custom URL protocol by subclassing NSURLProtocol, and it needs protocol-specific properties, extend NSURLRequest with accessor methods for those custom properties. In your accessor methods, call propertyForKey:inRequest: and setProperty:forKey:inRequest: to associate property values with the request.

Topics

Creating Requests

+ requestWithURL:

Creates and returns a URL request for a specified URL.

- initWithURL:

Creates a URL request for a specified URL.

+ requestWithURL:cachePolicy:timeoutInterval:

Creates and returns an initialized URL request with specified URL, cache policy, and timeout values.

- initWithURL:cachePolicy:timeoutInterval:

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

Working with a Cache Policy

cachePolicy

The request’s cache policy.

NSURLRequestCachePolicy

The constants used to specify interaction with the cached responses.

Accessing Request Components

HTTPMethod

The HTTP request method.

URL

The URL being requested.

HTTPBody

The request body.

HTTPBodyStream

The request body as an input stream.

mainDocumentURL

The main document URL associated with the request.

Getting Header Fields

allHTTPHeaderFields

A dictionary containing all of the HTTP header fields for a request.

- valueForHTTPHeaderField:

Returns the value of the specified HTTP header field.

Controlling Request Behavior

timeoutInterval

The request’s timeout interval, in seconds.

HTTPShouldHandleCookies

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

HTTPShouldUsePipelining

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

allowsCellularAccess

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

Supporting Limited Modes

allowsConstrainedNetworkAccess

A Boolean value that indicates whether connections may use the network when the user has specified Low Data Mode.

allowsExpensiveNetworkAccess

A Boolean value that indicates whether connections may use a network interface that the system considers expensive.

Accessing the Service Type

networkServiceType

The network service type of the request.

NSURLRequestNetworkServiceType

Constants that specify how a request uses network resources.

Supporting Secure Coding

supportsSecureCoding

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

Relationships

Inherits From

See Also

Requests and Responses

NSMutableURLRequest

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

NSURLResponse

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

NSHTTPURLResponse

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