<!--
{
  "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/URLSessionTaskDelegate/urlSession(_:task:needNewBodyStream:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(pl)NSURLSessionTaskDelegate(im)URLSession:task:needNewBodyStream:"
  },
  "title" : "urlSession(_:task:needNewBodyStream:)"
}
-->

# urlSession(_:task:needNewBodyStream:)

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

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

## 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.

## 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:)`](/documentation/Foundation/URLSession/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.

---

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)