38

I've been successfully using Invoke-WebRequest to post requests to a REST-based API from PowerShell.

Invoke-WebRequest -UseBasicParsing https://my-rest-api.com/endpoint -ContentType "application/json" -Method POST -Body $json

Today I came across Invoke-RestMethod which sounds more aptly-named for what I'm doing. What is the difference, and is there a reason to use one over the other?

2
  • Invoke-RestMethod has a different parameter set. In addition (always a bit hard to tell) it was probably introduced in a later PowerShell version.
    – Seth
    Jul 28, 2017 at 10:17
  • 2
    @Seth Both were introduced in version 3. This can be found on the Get-Help pages for both cmdlets. I'd speculate that Invoke-RestMethod was technically published first, since the "Online Version" link in the Get-Help ends with a smaller number than that found on Invoke-WebRequest's page.
    – root
    Jul 28, 2017 at 13:25

2 Answers 2

40

You can find out by decompiling the Microsoft.PowerShell.Commands.Utility assembly.

Basically, Invoke-WebRequest does not deal with parsing the data all that much. With -UseBasicParsing, it does some Regex-based HTML parsing. Without this switch, it’ll use the Internet Explorer COM API to parse the document.

That’s it. It’ll always attempt to parse HTML.

Invoke-RestMethod on the other hand has code to support JSON and XML content. It’ll attempt to detect an appropriate decoder. It does not support HTML (except for XML-compliant HTML, of course).

Both share the same core logic to make the actual HTTP request. It’s only in result processing that they differ.

Seeing is believing!

PS C:\Users\fuzzy> (Invoke-RestMethod https://httpbin.org/headers).headers

Connection Host        User-Agent
---------- ----        ----------
close      httpbin.org Mozilla/5.0 (Windows NT; Windows NT 10.0; de-DE) WindowsPowerShell/5.1.15063.483

PS C:\Users\fuzzy> Invoke-WebRequest -UseBasicParsing https://httpbin.org/headers


StatusCode        : 200
StatusDescription : OK
Content           : {
                      "headers": {
                        "Connection": "close",
                        "Host": "httpbin.org",
                        "User-Agent": "Mozilla/5.0 (Windows NT; Windows NT 10.0; de-DE)
                    WindowsPowerShell/5.1.15063.483"
                      }
                    }

RawContent        : HTTP/1.1 200 OK
                    Connection: keep-alive
                    Access-Control-Allow-Origin: *
                    Access-Control-Allow-Credentials: true
                    X-Processed-Time: 0.00075101852417
                    Content-Length: 180
                    Content-Type: application/json...
Forms             :
Headers           : {[Connection, keep-alive], [Access-Control-Allow-Origin, *], [Access-Control-Allow-Credentials,
                    true], [X-Processed-Time, 0.00075101852417]...}
Images            : {}
InputFields       : {}
Links             : {}
ParsedHtml        :
RawContentLength  : 180
10

systemcenterautomation.com did a blog post about this. The conclusion:

Invoke-RestMethod is much better at dealing with XML and JSON results, while Invoke-WebRequest is better at dealing with straight HTML results

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .