none
Replace Invoke-RestMethod in PowerShell 2.0 RRS feed

  • Question

  • Hello everyone,

    I'm using

    Invoke-RestMethod -Uri $URL -Method Get

    in my script, but this cmdlet in not available in PS 2.0 and unfortunately I'm stuck in envinronment with this version.

    Is there solution how to replace Invoke-RestMethod cmdlet in PS 2.0? Possibly using .NET classes?

    Thank you

    Tuesday, June 30, 2015 8:20 AM

Answers

  • Hi,

    I tried the following in a PowerShell 2 session.  The URL I'm using is a test REST API for Odata, the idea being to prove that I can get a connection and retrieve some data:

    $WebRequest = [System.Net.WebRequest]::Create("http://services.odata.org/v4/TripPinServiceRW/People")
    $WebRequest.Method = "GET"
    $WebRequest.ContentType = "application/json"
    $Response = $WebRequest.GetResponse()
    $ResponseStream = $Response.GetResponseStream()
    $ReadStream = New-Object System.IO.StreamReader $ResponseStream
    $Data=$ReadStream.ReadToEnd()

    After running those commands, I end up with a bunch of JSON data in $Data.  Does that work for you?

    Cheers,
    Mark.

    • Marked as answer by Mario199109 Wednesday, July 1, 2015 1:56 PM
    Tuesday, June 30, 2015 11:25 AM
  • $request = [System.Net.WebRequest]::Create("$url")
    $request.ContentType='application/json; charset=utf-8'
    $request.GetResponse()


    \_(ツ)_/


    • Edited by jrv Tuesday, June 30, 2015 11:11 AM
    • Marked as answer by Mario199109 Wednesday, July 1, 2015 1:56 PM
    Tuesday, June 30, 2015 11:11 AM

All replies

  • Hi Mario,

    It looks like System.Net.WebRequest is your friend :-)  I found a few good articles by searching for alternatives to Invoke-WebRequest (Invoke-RestMethod's big brother!)

    I found this on GitHub Gists:

    https://gist.github.com/nolim1t/271018

    This on powershell.org:

    http://powershell.org/wp/forums/topic/invoke-webrequest-powershell-2-0/

    This one from Stackoverflow:

    http://stackoverflow.com/questions/7715695/http-requests-with-powershell/20941552

    All of those answers have System.Net.WebRequest in common, so I'm guessing that's your answer.

    Cheers,
    Mark.

    Tuesday, June 30, 2015 9:27 AM
  • Hi Mark,

    thank you for your reply. There is one thing I forgot to mention. URL on which I need to perform GET method is "RESTful API".

    I tried something like this:

    $request = [System.Net.WebRequest]::Create("$url")
    $request.Method = 'GET'
    $request.GetResponse()

    But i received Exception calling "GetResponse" with "0" argument(s): "The underlying connection was closed: An unexpected error occurred on a send."

    Sorry if this everything sound little silly but I don't have knowledge around these web things. I'm just looking for simple solution that can replace Invoke-RestMethod which does not required any knowledge from me.

    Tuesday, June 30, 2015 10:51 AM
  • $request = [System.Net.WebRequest]::Create("$url")
    $request.ContentType='application/json; charset=utf-8'
    $request.GetResponse()


    \_(ツ)_/


    • Edited by jrv Tuesday, June 30, 2015 11:11 AM
    • Marked as answer by Mario199109 Wednesday, July 1, 2015 1:56 PM
    Tuesday, June 30, 2015 11:11 AM
  • Hi,

    I tried the following in a PowerShell 2 session.  The URL I'm using is a test REST API for Odata, the idea being to prove that I can get a connection and retrieve some data:

    $WebRequest = [System.Net.WebRequest]::Create("http://services.odata.org/v4/TripPinServiceRW/People")
    $WebRequest.Method = "GET"
    $WebRequest.ContentType = "application/json"
    $Response = $WebRequest.GetResponse()
    $ResponseStream = $Response.GetResponseStream()
    $ReadStream = New-Object System.IO.StreamReader $ResponseStream
    $Data=$ReadStream.ReadToEnd()

    After running those commands, I end up with a bunch of JSON data in $Data.  Does that work for you?

    Cheers,
    Mark.

    • Marked as answer by Mario199109 Wednesday, July 1, 2015 1:56 PM
    Tuesday, June 30, 2015 11:25 AM
  • Thank you guys, it works!
    Wednesday, July 1, 2015 1:56 PM
  • Can you please provide some insight how to do rest call using userid/password and with Basic Auth?
    Wednesday, April 11, 2018 3:00 PM
  • Can you please provide some insight how to do rest call using userid/password and with Basic Auth?

    See: https://blog.restcase.com/restful-api-authentication-basics/

    Please do not add questions to other users threads.  Open your own question.

    Also this is a very old and closed thread.


    \_(ツ)_/

    Wednesday, April 11, 2018 3:03 PM
  • thanks

    I will open another question. My question was in similar line hence asked in this thread. I wanted to know Rest call in PS with Basic auth.

    Wednesday, April 11, 2018 3:18 PM