<!--
{
  "availability" : [
    "iOS: 7.0.0 -",
    "iPadOS: 7.0.0 -",
    "macCatalyst: 13.1.0 -",
    "macOS: 10.9.0 -",
    "tvOS: 9.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 2.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Foundation",
  "identifier" : "/documentation/Foundation/URLSession/dataTask(with:)-7jpys",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(cs)NSURLSession(im)dataTaskWithRequest:"
  },
  "title" : "dataTask(with:)"
}
-->

# dataTask(with:)

Creates a task that retrieves the contents of a URL based on the specified URL request object.

```
func dataTask(with request: URLRequest) -> URLSessionDataTask
```

## Parameters

`request`

A URL request object that provides request-specific information such as the URL, cache policy, request type, and body data or body stream.

## Return Value

The new session data task.

## Discussion

By creating a task based on a request object, you can tune various aspects of the task’s behavior, including the cache policy and timeout interval.

After you create the task, you must start it by calling its [`resume()`](/documentation/Foundation/URLSessionTask/resume()) method.

---

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)