Reputation:
What is the equivalent to Invoke-WebRequest function in PowerShell Version 2. This is what I am trying to doing with my function as I cannot upgrade to PowerShell 4 because I am working on Windows Server 2003.
Invoke-WebRequest $uri -Body ($baseMessage | ConvertTo-Json -Compress) -Method Post -ContentType "application/json"
Thank you
Upvotes: 0
Views: 3505
Reputation: 3111
$Web = New-Object System.Net.WebClient
$Web.OpenRead($url)
$PageContents = New-Object System.IO.StreamReader($Web)
$PageContents.ReadToEnd()
If you're looking to submit JSON data you can use this instead:
$Encoding = [System.Text.Encoding]::GetEncoding("ASCII")
$postArray = $Encoding.GetBytes($json)
$Web = [System.Net.WebRequest]::Create($url)
$Web.Method = "POST"
$Web.Timeout = 10000;
$Stream = $Web.GetRequestStream()
$Stream.Write($postArray, 0, $postArray.Length)
$Web.GetResponse()
https://msdn.microsoft.com/en-us/library/System.Net.WebClient_methods(v=vs.80).aspx
Found another similar question on stackoverflow:
Upvotes: 3
Reputation: 2475
You can use System.Net.Webrequest
.NET class in powershell v2 instead.
See this example from one of my powershell answers: Powershell - View Website Source Information
And also answer to this shows how to set json content type, albeit in C# How to get json response using system.net.webrequest in c#?
Upvotes: 1