Structure

URLRequest

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

Declaration

struct URLRequest

Overview

URLRequest 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.

URLRequest only represents information about the request. Use other classes, such as URLSession, 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.

When writing Swift code, favor this structure over the NSURLRequest and NSMutableURLRequest classes.

Certain header fields are reserved; see Reserved HTTP Headers.

Topics

Creating a Request

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

Creates and initializes a URL request with the given URL, cache policy, and timeout interval.

Working with a Cache Policy

var cachePolicy: URLRequest.CachePolicy

The request’s cache policy.

typealias URLRequest.CachePolicy

An alias for the 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 of the request.

var httpBody: Data?

The data sent as the message body of a request, such as for an HTTP POST request.

var httpBodyStream: InputStream?

The stream used to deliver the HTTP body.

var mainDocumentURL: URL?

The main document URL associated with this request.

Accessing Header Fields

var allHTTPHeaderFields: [String : String]?

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

func addValue(String, forHTTPHeaderField: String)

Adds a value to the header field.

func setValue(String?, forHTTPHeaderField: String)

Sets a value for the header field.

Controlling Request Behavior

var timeoutInterval: TimeInterval

The timeout interval of the request.

var httpShouldHandleCookies: Bool

A Boolean value indicating whether cookies will be sent with and set for this request.

var httpShouldUsePipelining: Bool

A Boolean value indicating whether the request should transmit before the previous response is received.

var allowsCellularAccess: Bool

A Boolean value indicating whether the request is allowed to use the built-in cellular radios to satisfy the request.

Supporting Limited Modes

var allowsConstrainedNetworkAccess: Bool

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

var allowsExpensiveNetworkAccess: Bool

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

Accessing the Service Type

var networkServiceType: URLRequest.NetworkServiceType

The service type associated with this request.

typealias URLRequest.NetworkServiceType

An alias for the network service type.

enum NSURLRequest.NetworkServiceType

Constants that specify how a request uses network resources.

Comparing Requests

static func != (URLRequest, URLRequest) -> Bool

Returns a Boolean value indicating whether two values are not equal.

static func == (URLRequest, URLRequest) -> Bool

Indicates whether two URL requests are the same.

Describing Requests

var description: String

A textual description of the request.

var debugDescription: String

A textual description of the request suitable for debugging.

var customMirror: Mirror

A mirror that reflects the request.

var hashValue: Int

The computed hash value for the request.

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.

class NSMutableURLRequest

A mutable URL load request that bridges to NSURLRequest and 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.

See Also

Requests and Responses

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.