Software Development Kit (SDK) and API Discussions

Trouble with Powershell script to call HTTP POST

ag
NetApp
4,090 Views

Hi,

 

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: http://10.63.119.80/rest/docs/resource_BackupResource.html

 

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 system.net.networkcredential("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)

    $streamWriter.Write($body)

    $streamWriter.flush()

    $streamWriter.close()

    

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

    $responseStream = $response.GetResponseStream()

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

    $xmlDump = $stream.ReadToEnd()

    $output = [xml]$xmlDump

    $response.close()

    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:

http://10.63.119.80/rest/backups

 

Thanks,

Anil

0 REPLIES 0
Public