---
title: NSURLRequest
framework: foundation
role: symbol
role_heading: Class
path: foundation/nsurlrequest
---

# NSURLRequest

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

## Declaration

```swift
class NSURLRequest
```

## Overview

Overview Use this type in Swift when you need reference semantics or other Foundation-specific behavior. 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 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. The mutable subclass of NSURLRequest is NSMutableURLRequest. important: The Swift overlay to the Foundation framework provides the URLRequest structure, which bridges to the NSURLRequest class and its mutable subclass, NSMutableURLRequest. For more information about value types, see 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, and it needs protocol-specific properties, extend NSURLRequest with accessor methods for those custom properties. In your accessor methods, call property(forKey:in:) and setProperty(_:forKey:in:) to associate property values with the request.

## Topics

### Creating requests

- [init(url:)](foundation/nsurlrequest/init(url:)-7dmpd.md)
- [init(url:cachePolicy:timeoutInterval:)](foundation/nsurlrequest/init(url:cachepolicy:timeoutinterval:)-2giyj.md)

### Working with a cache policy

- [cachePolicy](foundation/nsurlrequest/cachepolicy-swift.property.md)
- [NSURLRequest.CachePolicy](foundation/nsurlrequest/cachepolicy-swift.enum.md)

### Accessing request components

- [httpMethod](foundation/nsurlrequest/httpmethod.md)
- [url](foundation/nsurlrequest/url.md)
- [httpBody](foundation/nsurlrequest/httpbody.md)
- [httpBodyStream](foundation/nsurlrequest/httpbodystream.md)
- [mainDocumentURL](foundation/nsurlrequest/maindocumenturl.md)

### Getting header fields

- [allHTTPHeaderFields](foundation/nsurlrequest/allhttpheaderfields.md)
- [value(forHTTPHeaderField:)](foundation/nsurlrequest/value(forhttpheaderfield:).md)

### Controlling request behavior

- [timeoutInterval](foundation/nsurlrequest/timeoutinterval.md)
- [httpShouldHandleCookies](foundation/nsurlrequest/httpshouldhandlecookies.md)
- [httpShouldUsePipelining](foundation/nsurlrequest/httpshouldusepipelining.md)
- [allowsCellularAccess](foundation/nsurlrequest/allowscellularaccess.md)

### Supporting limited modes

- [allowsConstrainedNetworkAccess](foundation/nsurlrequest/allowsconstrainednetworkaccess.md)
- [allowsExpensiveNetworkAccess](foundation/nsurlrequest/allowsexpensivenetworkaccess.md)

### Accessing the service type

- [networkServiceType](foundation/nsurlrequest/networkservicetype-swift.property.md)
- [NSURLRequest.NetworkServiceType](foundation/nsurlrequest/networkservicetype-swift.enum.md)

### Supporting secure coding

- [supportsSecureCoding](foundation/nsurlrequest/supportssecurecoding.md)

### Indicating the source of the request

- [attribution](foundation/nsurlrequest/attribution-swift.property.md)
- [NSURLRequest.Attribution](foundation/nsurlrequest/attribution-swift.enum.md)

### Initializers

- [init(URL:)](foundation/nsurlrequest/init(url:)-9mck0.md)
- [init(URL:)](foundation/nsurlrequest/init(url:)-9plp8.md)
- [init(URL:cachePolicy:timeoutInterval:)](foundation/nsurlrequest/init(url:cachepolicy:timeoutinterval:)-59r40.md)
- [init(URL:cachePolicy:timeoutInterval:)](foundation/nsurlrequest/init(url:cachepolicy:timeoutinterval:)-7xiv0.md)
- [init(coder:)](foundation/nsurlrequest/init(coder:).md)

### Instance Properties

- [allowsPersistentDNS](foundation/nsurlrequest/allowspersistentdns.md)
- [allowsUltraConstrainedNetworkAccess](foundation/nsurlrequest/allowsultraconstrainednetworkaccess.md)
- [assumesHTTP3Capable](foundation/nsurlrequest/assumeshttp3capable.md)
- [cookiePartitionIdentifier](foundation/nsurlrequest/cookiepartitionidentifier.md)
- [requiresDNSSECValidation](foundation/nsurlrequest/requiresdnssecvalidation.md)

## Relationships

### Inherits From

- [NSObject](objectivec/nsobject-swift.class.md)

### Inherited By

- [NSMutableURLRequest](foundation/nsmutableurlrequest.md)

### Conforms To

- [CVarArg](swift/cvararg.md)
- [Copyable](swift/copyable.md)
- [CustomDebugStringConvertible](swift/customdebugstringconvertible.md)
- [CustomStringConvertible](swift/customstringconvertible.md)
- [Equatable](swift/equatable.md)
- [Escapable](swift/escapable.md)
- [Hashable](swift/hashable.md)
- [NSCoding](foundation/nscoding.md)
- [NSCopying](foundation/nscopying.md)
- [NSMutableCopying](foundation/nsmutablecopying.md)
- [NSObjectProtocol](objectivec/nsobjectprotocol.md)
- [NSSecureCoding](foundation/nssecurecoding.md)

## See Also

### Requests and responses

- [URLRequest](foundation/urlrequest.md)
- [NSMutableURLRequest](foundation/nsmutableurlrequest.md)
- [URLResponse](foundation/urlresponse.md)
- [HTTPURLResponse](foundation/httpurlresponse.md)
