# REST api call using Start-BitsTransfer

**URL:** <https://forums.powershell.org/t/rest-api-call-using-start-bitstransfer/19701>\
**Category:** PowerShell Help\
**Created:** [June 24, 2022, 1:29pm UTC](https://forums.powershell.org/t/rest-api-call-using-start-bitstransfer/19701 "2022-06-24T13:29:57Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![RajD](https://avatars.discourse-cdn.com/v4/letter/r/d26b3c/32.png) [@RajD](https://forums.powershell.org/u/RajD)\
**Post date:** [June 24, 2022, 1:29pm UTC](https://forums.powershell.org/t/rest-api-call-using-start-bitstransfer/19701/1 "2022-06-24T13:29:57Z")

</div>

Hi, I’m working on re-writing my code to make a REST api call Start-BitsTransfer instead of the `Invoke-RestMethod @params | ConvertTo-Json | Out-File $DestFile` and I would like to have my payload saved as JSON document. `Invoke-RestMethod` works fine when the data volume is not too big. When the volume of data gets bigger I’m running into all sorts of performance issues.

```auto
$Header = @{
    "authorization" = "Bearer $token"
}
#make REST API call
$Parameters = @{
    Method = "GET"
    Uri = "https://api.mysite.com/v1/data"
    Headers = $Header
    ContentType = "application/json"
    Body = $BodyJson
}
Start-BitsTransfer -Priority High -TransferType Download -Source $Parameters -Destination $DestFile | Complete-BitsTransfer;

```

**Error:**

```auto
Start-BitsTransfer : Cannot find path 'C:\Users\uname\System.Collections.Hashtable' because it does not exist.
At line:62 char:5
+ Start-BitsTransfer -Priority High -TransferType Download -Source ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : ObjectNotFound: (C:\Users\uname...tions.Hashtable:String) [Start-BitsTransfer], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.BackgroundIntelligentTransfer.Management.NewBitsTransferCommand`Preformatted text`

```

Thank you

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [June 24, 2022, 1:52pm UTC](https://forums.powershell.org/t/rest-api-call-using-start-bitstransfer/19701/2 "2022-06-24T13:52:58Z")

</div>

The parameter `-Source` takes a string or an array of strings with the source file paths. If you have more than one file to download you have to either provide a list of files or use wildcards.

> **[Start-BitsTransfer (BitsTransfer)](https://docs.microsoft.com/en-us/powershell/module/bitstransfer/start-bitstransfer?view=windowsserver2022-ps)**
>
> Use this topic to help manage Windows and Windows Server technologies with Windows PowerShell.

---

<div class="post-metadata">

**Author:** ![RajD](https://avatars.discourse-cdn.com/v4/letter/r/d26b3c/32.png) [@RajD](https://forums.powershell.org/u/RajD)\
**Post date:** [June 24, 2022, 2:25pm UTC](https://forums.powershell.org/t/rest-api-call-using-start-bitstransfer/19701/3 "2022-06-24T14:25:58Z")

</div>

@Olaf , I have only one file to download. Not too sure why I get that error.

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [June 24, 2022, 2:33pm UTC](https://forums.powershell.org/t/rest-api-call-using-start-bitstransfer/19701/4 "2022-06-24T14:33:24Z")

</div>

> [@RajD](#):
>
> Not too sure why I get that error.

Because you provided a hastable instead of a string. For the parameter `-Source` you have to provide a complete file path.

---

<div class="post-metadata">

**Author:** ![RajD](https://avatars.discourse-cdn.com/v4/letter/r/d26b3c/32.png) [@RajD](https://forums.powershell.org/u/RajD)\
**Post date:** [June 24, 2022, 2:57pm UTC](https://forums.powershell.org/t/rest-api-call-using-start-bitstransfer/19701/5 "2022-06-24T14:57:35Z")

</div>

Hi @Olaf , How do I pass an URL with parameters to the source in order to make the api call.

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [June 24, 2022, 3:12pm UTC](https://forums.powershell.org/t/rest-api-call-using-start-bitstransfer/19701/6 "2022-06-24T15:12:17Z")

</div>

AFAIK you can’t. Please read the help completely including the examples. I’d try to play around with the parameters `-Authentication` and `-CustomHeaders`.

---

<div class="post-metadata">

**Author:** ![RajD](https://avatars.discourse-cdn.com/v4/letter/r/d26b3c/32.png) [@RajD](https://forums.powershell.org/u/RajD)\
**Post date:** [June 24, 2022, 8:39pm UTC](https://forums.powershell.org/t/rest-api-call-using-start-bitstransfer/19701/7 "2022-06-24T20:39:37Z")

</div>

Hi @Olaf , I am using `System.Net.WebClient` instead in the below code.

```auto
    $wc = New-Object System.Net.WebClient
    $wc.Headers.Add('Authorization', "Bearer $tokenurl")
    $temp = $wc.DownloadString($apiurl)
    $temp | ConvertTo-Json | Out-File $outfile

```

Please let me know your thoughts.

---

<div class="post-metadata">

**Author:** ![RajD](https://avatars.discourse-cdn.com/v4/letter/r/d26b3c/32.png) [@RajD](https://forums.powershell.org/u/RajD)\
**Post date:** [June 27, 2022, 4:57pm UTC](https://forums.powershell.org/t/rest-api-call-using-start-bitstransfer/19701/8 "2022-06-27T16:57:10Z")

</div>

Hi @Olaf I updated my code for error handling but I don’t capture the actual error message.

```auto
try
{
    $wc = New-Object System.Net.WebClient
    $wc.Headers.Add('Authorization', "Bearer $tokenurl")
    $temp = $wc.DownloadString($apiurl)
    $temp | ConvertTo-Json | Out-File $outfile
}
catch
{
    $ErrorMessage = $_.Exception.Message
    $FailedItem = $_.Exception.ItemName
    ((Get-Date).ToString() + " | Error: " + $ErrorMessage + " - " + $FailedItem);
}

```

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [June 27, 2022, 5:20pm UTC](https://forums.powershell.org/t/rest-api-call-using-start-bitstransfer/19701/9 "2022-06-27T17:20:28Z")

</div>

For the `try` block to work properly there has to be a terminating error. If the cmdlet you’re using does not raise a terminating error by default you have to force it with the parameter `-ErrorAcction Stop`. If you’re using a dotNet method instead of cmdlets it gets even more complex. I’d urgently recommend to stay with PowerShell cmdlets and only use dotNet or other methods if PowerShell cmdlets are not available or not suitable for your particular requirements.

To call a REST API there is the cmdlet

> **[Invoke-RestMethod (Microsoft.PowerShell.Utility) - PowerShell](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/invoke-restmethod?view=powershell-7.2)**
>
> The Invoke-RestMethod cmdlet sends HTTP and HTTPS requests to Representational State Transfer (REST) web services that return richly structured data. PowerShell formats the response based to the data type. For an RSS or ATOM feed, PowerShell returns...

Please read the help completely including the examples to learn how to use it.

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:17pm UTC](https://forums.powershell.org/t/rest-api-call-using-start-bitstransfer/19701/10 "2024-05-16T20:17:48Z")

</div>

Closed on May 16, 2024, 8:17pm UTC


