Powershell: Invoke-RestMethod vs Invoke-WebRequest

In Powershell, aside from the old school Net objects, we have Invoke-RestMethod and Invoke-WebRequest cmdlets to make HTTP/REST calls. I’ve been exploring both as part of my APIs and Powershell series. So far there are times when one is better than the other at certain things. Like Invoke-RestMethod turning JSON response files directly into Powershell objects, as shown in my post about weather data. Are there other areas when one is better than the other? Keep reading to find out more about Invoke-RestMethod vs Invoke-WebRequest.

 

Exploring Websites

In my exploration of multiple websites, if you have no documentation Invoke-WebRequest is a great start. Suppose we connect to the same website with both commands, where the variable $web is using Invoke-WebRequest and $Rest is using Invoke-RestMethod.

When we connect with webrequest and output that to a variable everything in the red box becomes a object we can access via the variable.

invoke-webrequest html

Example: getting the base response from the site.

invoke-webrequest html

And getting the headers. Notice when I do $rest.headers nothing is returned.

invoke-webrequest HTML

Here, we output the entire $rest result, which is essentially the entire HTML document.

invoke-restmethod vs invoke-webrequest

So in this instance, Invoke-webrequest is the better option, because I can create logic around the status code, or find other data in the fields because they are already Powershell objects.

 

Invoke-RestMethod for JSON, XML

If you read my weather data post, then you already know Invoke-Restmethod returns JSON files natively as Powershell objects. But it also does the same for XML content. We’ll use the same example as above, only getting a .xml document from the same website.

invoke-restmethod vs invoke-webrequest

As you can see, once again we have all the same fields as the previous page. But, if I want to get the content as Powershell objects by doing $web.content and assigning that to the variable $content, I can’t get them.

invoke-restmethod vs invoke-webrequest

$content.nutcstatus returns nothing. Because the result is a XML file we can tell Powershell that we are working with XML by putting before our variable.

invoke-webrequest XML

Now I have all the data in the Powershell pipeline. However, Invoke-Restmethod returns the resulting XML back as a powershell object natively.

invoke-restmethod XML

In one step I have the data I wanted, where Invoke-WebRequest required several extra steps, to ultimately get the same result.

 

Invoke-Restmethod vs Invoke-Webrequest

Invoke-RestMethod is much better at dealing with XML and JSON results, while Invoke-WebRequest is better at dealing with straight HTML results. I hope this post has helped you to determine when to use which command and what each command is capable of, at least when getting data from web endpoints. Bare in mind I didn’t go into posting data with either command. Maybe I’l do a post on that soon.

2 thoughts on “Powershell: Invoke-RestMethod vs Invoke-WebRequest”

Comments are closed.