<!--
{
  "availability" : [
    "iOS: 8.0.0 -",
    "iPadOS: 8.0.0 -",
    "macCatalyst: 8.0.0 -",
    "macOS: 10.10.0 -",
    "tvOS: 9.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 2.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Foundation",
  "identifier" : "/documentation/Foundation/URLRequest",
  "metadataVersion" : "0.1.0",
  "role" : "Structure",
  "symbol" : {
    "kind" : "Structure",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "s:10Foundation10URLRequestV"
  },
  "title" : "URLRequest"
}
-->

# URLRequest

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

```
struct URLRequest
```

## Overview

[`URLRequest`](/documentation/Foundation/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`](/documentation/Foundation/URLRequest) includes the HTTP method (`GET`, `POST`, and so on) and the HTTP headers.

[`URLRequest`](/documentation/Foundation/URLRequest) only represents information about the request. Use other classes, such as [`URLSession`](/documentation/Foundation/URLSession), to send the request to a server. See [Fetching website data into memory](/documentation/Foundation/fetching-website-data-into-memory) and [Uploading data to a website](/documentation/Foundation/uploading-data-to-a-website) for an introduction to these techniques.

When writing Swift code, favor this structure over the [`NSURLRequest`](/documentation/Foundation/NSURLRequest) and [`NSMutableURLRequest`](/documentation/Foundation/NSMutableURLRequest) classes.

Certain header fields are reserved; see [`Reserved HTTP headers`](/documentation/Foundation/NSURLRequest#Reserved-HTTP-headers).

## Topics

### Creating a request

[`init(url: URL, cachePolicy: URLRequest.CachePolicy, timeoutInterval: TimeInterval)`](/documentation/Foundation/URLRequest/init(url:cachePolicy:timeoutInterval:))

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

### Working with a cache policy

[`var cachePolicy: URLRequest.CachePolicy`](/documentation/Foundation/URLRequest/cachePolicy-swift.property)

The request’s cache policy.

[`typealias CachePolicy`](/documentation/Foundation/URLRequest/CachePolicy-swift.typealias)

An alias for the cache policy.

[`enum CachePolicy`](/documentation/Foundation/NSURLRequest/CachePolicy-swift.enum)

The constants used to specify interaction with the cached responses.

### Accessing request components

[`var httpMethod: String?`](/documentation/Foundation/URLRequest/httpMethod)

The HTTP request method.

[`var url: URL?`](/documentation/Foundation/URLRequest/url)

The URL of the request.

[`var httpBody: Data?`](/documentation/Foundation/URLRequest/httpBody)

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

[`var httpBodyStream: InputStream?`](/documentation/Foundation/URLRequest/httpBodyStream)

The stream used to deliver the HTTP body.

[`var mainDocumentURL: URL?`](/documentation/Foundation/URLRequest/mainDocumentURL)

The main document URL associated with this request.

### Accessing header fields

[`var allHTTPHeaderFields: [String : String]?`](/documentation/Foundation/URLRequest/allHTTPHeaderFields)

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

[`func addValue(String, forHTTPHeaderField: String)`](/documentation/Foundation/URLRequest/addValue(_:forHTTPHeaderField:))

Adds a value to the header field.

[`func setValue(String?, forHTTPHeaderField: String)`](/documentation/Foundation/URLRequest/setValue(_:forHTTPHeaderField:))

Sets a value for the header field.

[`func value(forHTTPHeaderField: String) -> String?`](/documentation/Foundation/URLRequest/value(forHTTPHeaderField:))

Retrieves a header value.

### Controlling request behavior

[`var timeoutInterval: TimeInterval`](/documentation/Foundation/URLRequest/timeoutInterval)

The timeout interval of the request.

[`var httpShouldHandleCookies: Bool`](/documentation/Foundation/URLRequest/httpShouldHandleCookies)

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

[`var httpShouldUsePipelining: Bool`](/documentation/Foundation/URLRequest/httpShouldUsePipelining)

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

[`var allowsCellularAccess: Bool`](/documentation/Foundation/URLRequest/allowsCellularAccess)

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

[`var allowsPersistentDNS: Bool`](/documentation/Foundation/URLRequest/allowsPersistentDNS)

`true` if the request is allowed to store and use DNS answers, potentially beyond TTL expiry, in a persistent
per-process cache, `false` otherwise. Defaults to `false`. This should only be set to
`true` for hostnames whose resolutions are not expected to change across networks.

[`var assumesHTTP3Capable: Bool`](/documentation/Foundation/URLRequest/assumesHTTP3Capable)

`true` if server endpoint is known to support HTTP/3. Enables QUIC racing
without HTTP/3 service discovery. Defaults to `false`.
The default may be `true` in a future OS update.

[`var cookiePartitionIdentifier: String?`](/documentation/Foundation/URLRequest/cookiePartitionIdentifier)

[`var requiresDNSSECValidation: Bool`](/documentation/Foundation/URLRequest/requiresDNSSECValidation)

`true` if the request is required to do DNSSEC validation during DNS lookup.
`false` otherwise. Defaults to `false`.

### Supporting limited modes

[`var allowsConstrainedNetworkAccess: Bool`](/documentation/Foundation/URLRequest/allowsConstrainedNetworkAccess)

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

[`var allowsExpensiveNetworkAccess: Bool`](/documentation/Foundation/URLRequest/allowsExpensiveNetworkAccess)

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`](/documentation/Foundation/URLRequest/networkServiceType-swift.property)

The type of network service for all tasks within network sessions to enable Cellular Network Slicing.

[`typealias NetworkServiceType`](/documentation/Foundation/URLRequest/NetworkServiceType-swift.typealias)

An alias for the network service type.

[`enum NetworkServiceType`](/documentation/Foundation/NSURLRequest/NetworkServiceType-swift.enum)

Constants that specify how a request uses network resources.

### Indicating the source of the request

[`var attribution: URLRequest.Attribution`](/documentation/Foundation/URLRequest/attribution-swift.property)

The entity that initiates the network request.

[`typealias Attribution`](/documentation/Foundation/URLRequest/Attribution-swift.typealias)

A type that indicates the entities that can make a network request.

### Using reference types

[`class NSURLRequest`](/documentation/Foundation/NSURLRequest)

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

[`class NSMutableURLRequest`](/documentation/Foundation/NSMutableURLRequest)

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

[`typealias MutableURLRequest`](/documentation/Foundation/MutableURLRequest)

### Instance Properties

[`var allowsUltraConstrainedNetworkAccess: Bool`](/documentation/Foundation/URLRequest/allowsUltraConstrainedNetworkAccess)

`true` if the receiver is allowed to use an interface marked as ultra-constrained to
satify the request, `false` otherwise.

## Relationships

### Conforms To

[`CustomStringConvertible`](/documentation/Swift/CustomStringConvertible)

[`Copyable`](/documentation/Swift/Copyable)

[`Equatable`](/documentation/Swift/Equatable)

[`ReferenceConvertible`](/documentation/Foundation/ReferenceConvertible)

[`CustomDebugStringConvertible`](/documentation/Swift/CustomDebugStringConvertible)

[`Sendable`](/documentation/Swift/Sendable)

[`Hashable`](/documentation/Swift/Hashable)

[`Escapable`](/documentation/Swift/Escapable)

[`CustomReflectable`](/documentation/Swift/CustomReflectable)

[`SendableMetatype`](/documentation/Swift/SendableMetatype)

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)