---
title: "dataTask(with:)"
framework: foundation
role: symbol
role_heading: Instance Method
path: "foundation/urlsession/datatask(with:)-7jpys"
---

# dataTask(with:)

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

## Declaration

```swift
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

Return Value The new session data task.

## Discussion

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() method.

## See Also

### Adding data tasks to a session

- [dataTask(with:)](foundation/urlsession/datatask(with:)-10dy7.md)
- [dataTask(with:completionHandler:)](foundation/urlsession/datatask(with:completionhandler:)-52wk8.md)
- [dataTask(with:completionHandler:)](foundation/urlsession/datatask(with:completionhandler:)-e6xv.md)
- [URLSessionDataTask](foundation/urlsessiondatatask.md)
- [URLSessionDataDelegate](foundation/urlsessiondatadelegate.md)
