---
title: "urlSession(_:task:needNewBodyStream:)"
framework: foundation
role: symbol
role_heading: Instance Method
path: "foundation/urlsessiontaskdelegate/urlsession(_:task:neednewbodystream:)"
---

# urlSession(_:task:needNewBodyStream:)

Tells the delegate when a task requires a new request body stream to send to the remote server.

## Declaration

```swift
optional func urlSession(_ session: URLSession, task: URLSessionTask, needNewBodyStream completionHandler: @escaping @Sendable (InputStream?) -> Void)
```

```swift
optional func urlSession(_ session: URLSession, needNewBodyStreamForTask task: URLSessionTask) async -> InputStream?
```

## Parameters

- `session`: The session containing the task that needs a new body stream.
- `task`: The task that needs a new body stream.
- `completionHandler`: A completion handler that your delegate method should call with the new body stream.

## Mentioned in

Uploading streams of data

## Discussion

Discussion The task calls this delegate method under two circumstances: To provide the initial request body stream if the task was created with uploadTask(withStreamedRequest:) To provide a replacement request body stream if the task needs to resend a request that has a body stream because of an authentication challenge or other recoverable server error. note: You don’t need to implement this method if your code provides the request body using a file URL or a data object.

## See Also

### Working with upload tasks

- [urlSession(_:task:didSendBodyData:totalBytesSent:totalBytesExpectedToSend:)](foundation/urlsessiontaskdelegate/urlsession(_:task:didsendbodydata:totalbytessent:totalbytesexpectedtosend:).md)
