How to Call a REST Method From Powershell | by Brian Olson | May, 2022

With a pokemon API instance

Photo by Pixabay from Pexels

Disclaimer: All opinions are my very own

Some time again I wrote about utilizing Powershell’s useful Invoke-WebRequest technique and bypassing the certificates verify, however there’s one other helpful Powershell technique associated to invoking internet strategies: Invoke-RestMethod

The complete docs are here, but it surely’s similar to Invoke-WebRequest with the distinction of what it reveals you by default.

Let’s take a look at a few examples of PokeAPI.

After I run Invoke-WebRequest on the PokeAPI:

Invoke-WebRequest https://pokeapi.co/api/v2/pokemon/ditto

I get again a response with plenty of data about the request, however not a lot data from the request

I can form of see it embedded within the content material, and if I’m not being too lazy I can see a straightforward strategy to pull that out and parse it.

(Invoke-WebRequest https://pokeapi.co/api/v2/pokemon/ditto).Content | ConvertFrom-json

However I’m a programmer and we’re inherently lazy. So let’s strive the identical factor with Invoke-RestMethod

Invoke-RestMethod https://pokeapi.co/api/v2/pokemon/ditto

And now I instantly have the entire content material parsed and obtainable.

This type of illustration is useful if you wish to proceed utilizing the outcome to do additional exploration like retrieving the entire particulars for the Pokemon’s species:

$pokemon = Invoke-RestMethod https://pokeapi.co/api/v2/pokemon/ditto;
Invoke-RestMethod $pokemon.species.url;

And there you may have it — invoking a relaxation technique with powershell. Completely happy powershelling!

More Posts