Search code examples
.netpowershellpowershell-2.0invoke

Powershell 2.0 Webrequest POST


I am a beginner in Powershell and was wondering if someone can help me with the following please.

$baseurl = "some url address"
$Body = @{
    jsonrpc = "2.0"
    method = "user.login"
    params = @{
        user = "username"
        password = "password"
    }
    id = 1
    auth = $null
}

$BodyJSON = ConvertTo-Json $Body
write-host $BodyJSON
try {
    $zabSession = Invoke-RestMethod ("$baseurl/api_jsonrpc.php") -ContentType "application/json" -Body $BodyJSON -Method Post | `
    Select-Object jsonrpc,@{Name="session";Expression={$_.Result}},id,@{Name="URL";Expression={$baseurl}}

The problem is i need the above converted to use in powershell 2.0 as invoke-restmethod does not work. Can someone please provide code or something to help please I am massively struggling. Thank you very much!


Solution

  • In order to implement similar functionality to the Invoke-RestMethod cmdlet in PowerShell v2, I believe you need to use the .NET System.Net.WebRequest & System.IO.StreamWriter/StreamReader class. Here is a quick function that mimics the basic functionality of the Invoke-RestMethod cmdlet.

    function InvokeRest {
      [CmdletBinding()]
      Param (
        [Parameter(Mandatory = $true, Position = 0)]
        [string]$URI,
    
        [Parameter(Mandatory = $true, Position = 1)]
        [string]$ContentType,
    
        [Parameter(Mandatory = $false, Position = 2)]
        [string]$Body,
    
        [Parameter(Mandatory = $false, Position = 3)]
        [ValidateSet('POST', 'GET')] #You can extend this to all the System.Net.WebRequest methods.
        [string]$Method = 'POST'
      )
    
      try {
        $restRequest = [System.Net.WebRequest]::Create($URI)
        $restRequest.ContentType = $ContentType
        $restRequest.Method = $Method
    
        if ($Method -eq 'POST') {
          $encoding = [System.Text.Encoding]::UTF8
    
          $restRequestStream = $restRequest.GetRequestStream()
          $restRequestWriter = New-Object System.IO.StreamWriter($restRequestStream, $encoding)
          
          $restRequestWriter.Write($Body)
        }
      }
      finally {
        if ($null -ne $restStream) { $restRequestStream.Dispose() }
        if ($null -ne $restWriter) { $restRequestWriter.Dispose() }
      }
    
      try {
        $restResponse = $restRequest.GetResponse()
        $restResponseStream = $restResponse.GetResponseStream()
    
        $responseStreamReader = New-Object System.IO.StreamReader($restResponseStream)
    
        $responseString = $responseStreamReader.ReadToEnd()
      }
      finally {
        if ($null -ne $restResponse) { $restResponse.Dispose() }
        if ($null -ne $restResponseStream) { $restResponseStream.Dispose() }
        if ($null -ne $responseStreamReader) { $responseStreamReader.Dispose() }
      }
    
      return $responseString
    }
    

    This function will return the response as a string. You can then convert the string to the appropriate type (ConvertFrom-Json/ConvertFrom-Yaml/custom converter).