Software Development Kit (SDK) and API Discussions

Trouble with Powershell script to call HTTP POST




I am using WFA REST APIs and one of the API there is to invoke a restore operation on WFA.

More details about the API can be found here:


I am not able to figure out a way to invoke an HTTP POST call by passing content of type multipart/form-data.

the type of file(backup file) i am trying to upload is gzip or zip file.


The script i am using is:


function invoke-rest {

    param([string]$uri, [string]$body )

    [System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}

          #$enc = [system.Text.Encoding]::UTF8

    $request = [System.Net.HttpWebRequest]::Create($uri)

    $request.Credentials = New-Object"admin","admin")

    $request.CookieContainer = New-Object System.Net.CookieContainer

    $boundary = "--------------"+(get-date -format yyyymmddhhmmss).ToString()


          $request.ContentType = "application/octet-stream; charset=UTF-8;"

    $request.Method = "Put"

    $request.keepAlive = $true

    $requestStream = $request.GetRequestStream()

    $streamWriter = New-Object System.IO.StreamWriter($requestStream)





    [net.httpWebResponse]$response = $request.GetResponse()

    $responseStream = $response.GetResponseStream()

    $stream = new-object System.IO.StreamReader $responseStream

    $xmlDump = $stream.ReadToEnd()

    $output = [xml]$xmlDump


    return $output


$uri = "http://localhost/rest/backups"

$method = "POST"

$body = @{}

$body = @{"name"="backupFile"; "filename"=[IO.File]::ReadAllBytes("C:\Users\Administrator\Desktop\wfa_backup_AG-1_.sql.gz")}

invoke-rest $uri $body


The error i get is:

Exception calling "GetResponse" with "0" argument(s): "The remote server returned an error: (500) Internal Server Error


At C:\Users\Administrator\Desktop\restore.ps1:19 char:58

+     [net.httpWebResponse]$response = $request.GetResponse <<<< ()

    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException

    + FullyQualifiedErrorId : DotNetMethodException



Any help is appreciated. You can also test a script here:




Earn Rewards for Your Review!
GPI Review Banner
All Community Forums