<!--
{
  "availability" : [
    "iOS: 2.0.0 -",
    "iPadOS: 2.0.0 -",
    "macCatalyst: 13.1.0 -",
    "macOS: 10.2.0 -",
    "tvOS: 9.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 2.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Foundation",
  "identifier" : "/documentation/Foundation/NSURLRequest",
  "metadataVersion" : "0.1.0",
  "role" : "Class",
  "symbol" : {
    "kind" : "Class",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(cs)NSURLRequest"
  },
  "title" : "NSURLRequest"
}
-->

# NSURLRequest

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

```
class NSURLRequest
```

## Overview

Use this type in Swift when you need reference semantics or other Foundation-specific behavior.

[`NSURLRequest`](/documentation/Foundation/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`](/documentation/Foundation/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`](/documentation/Foundation/NSURLRequest#Custom-protocol-properties).

[`NSURLRequest`](/documentation/Foundation/NSURLRequest) 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.

The mutable subclass of [`NSURLRequest`](/documentation/Foundation/NSURLRequest) is [`NSMutableURLRequest`](/documentation/Foundation/NSMutableURLRequest).

> Important:
> The Swift overlay to the Foundation framework provides the ``doc://com.apple.foundation/documentation/Foundation/URLRequest`` structure, which bridges to the ``doc://com.apple.foundation/documentation/Foundation/NSURLRequest`` class and its mutable subclass, ``doc://com.apple.foundation/documentation/Foundation/NSMutableURLRequest``. For more information about value types, see <doc://com.apple.documentation/documentation/Swift/working-with-foundation-types>.

### 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 [`URLProtocol`](/documentation/Foundation/URLProtocol), and it needs protocol-specific properties, extend [`NSURLRequest`](/documentation/Foundation/NSURLRequest) with accessor methods for those custom properties. In your accessor methods, call [`property(forKey:in:)`](/documentation/Foundation/URLProtocol/property(forKey:in:)) and [`setProperty(_:forKey:in:)`](/documentation/Foundation/URLProtocol/setProperty(_:forKey:in:)) to associate property values with the request.

## Topics

### Creating requests

[`requestWithURL:`](/documentation/Foundation/NSURLRequest/requestWithURL:)

Creates and returns a URL request for a specified URL.

[`init(url:)`](/documentation/Foundation/NSURLRequest/init(url:)-7dmpd)

Creates a URL request for a specified URL.

[`requestWithURL:cachePolicy:timeoutInterval:`](/documentation/Foundation/NSURLRequest/requestWithURL:cachePolicy:timeoutInterval:)

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

[`init(url:cachePolicy:timeoutInterval:)`](/documentation/Foundation/NSURLRequest/init(url:cachePolicy:timeoutInterval:)-2giyj)

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

### Working with a cache policy

[`cachePolicy`](/documentation/Foundation/NSURLRequest/cachePolicy-swift.property)

The request’s cache policy.

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

The constants used to specify interaction with the cached responses.

### Accessing request components

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

The HTTP request method.

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

The URL being requested.

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

The request body.

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

The request body as an input stream.

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

The main document URL associated with the request.

### Getting header fields

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

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

[`value(forHTTPHeaderField:)`](/documentation/Foundation/NSURLRequest/value(forHTTPHeaderField:))

Returns the value of the specified HTTP header field.

### Controlling request behavior

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

The request’s timeout interval, in seconds.

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

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

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

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

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

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

### Supporting limited modes

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

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

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

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

### Accessing the service type

[`networkServiceType`](/documentation/Foundation/NSURLRequest/networkServiceType-swift.property)

The network service type of the request.

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

Constants that specify how a request uses network resources.

### Supporting secure coding

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

A Boolean value indicating whether the [`NSURLRequest`](/documentation/Foundation/NSURLRequest) implements the [`NSSecureCoding`](/documentation/Foundation/NSSecureCoding) protocol.

### Indicating the source of the request

[`attribution`](/documentation/Foundation/NSURLRequest/attribution-swift.property)

The entity that initiates the network request.

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

The entities that can make a network request.



---

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)