In this article we give you examples of working with the Invoke-RestMethod cmdlet in Powershell

The Invoke-RestMethod cmdlet sends HTTP and HTTPS requests to Representational State Transfer (REST) web services that return richly structured data.

PowerShell formats the response based to the data type. For an RSS or ATOM feed, PowerShell returns the Item or Entry XML nodes. For JavaScript Object Notation (JSON) or XML, PowerShell converts, or deserializes, the content into [PSCustomObject] objects.

The documentation from microsoft is the best place to look at for in depth information

Now we will look at one of the easiest apis that I have found to use which is http://numbersapi.com which ias an api with a variety of number facts where you can choose by trivia, math, date, or year

 

Examples

 

Invoke-RestMethod http://numbersapi.com/6

This is what I saw

PS J:\windows> Invoke-RestMethod http://numbersapi.com/6
6 is the number of strings on a standard guitar.

 

Invoke-RestMethod "http://numbersapi.com/random?min=5&max=25"

This is what I saw

PS J:\windows> Invoke-RestMethod “http://numbersapi.com/random?min=5&max=25”
8 is the number of furlongs in a mile.
PS J:\windows> Invoke-RestMethod “http://numbersapi.com/random?min=5&max=25”
24 is the number of hours in a day.

 

Invoke-RestMethod http://numbersapi.com/1/29/date

This is what I saw running this twice

PS J:\windows> Invoke-RestMethod http://numbersapi.com/1/29/date
January 29th is the day in 1996 that President Jacques Chirac announces a “definitive end” to French nuclear weapons testing.
PS J:\windows> Invoke-RestMethod http://numbersapi.com/1/29/date
January 29th is the day in 1861 that Kansas is admitted as the 34th U.S. state.
PS J:\windows>

 

Invoke-RestMethod http://numbersapi.com/random/trivia

This is what I saw when I ran this a couple of times

PS J:\windows> Invoke-RestMethod http://numbersapi.com/random/trivia
6000000000000000000 is the number of transistor produced worldwide in 2008.
PS J:\windows> Invoke-RestMethod http://numbersapi.com/random/trivia
2701 is a plot triviality in Neal Stephenson’s “Cryptonomicon”.
PS J:\windows>

 

Invoke-RestMethod http://numbersapi.com/random/year

This is the output that I saw

PS J:\windows> Invoke-RestMethod http://numbersapi.com/random/year
624 is the year that Justus becomes Archbishop of Canterbury.
PS J:\windows> Invoke-RestMethod http://numbersapi.com/random/year
58 BC is the year that Berenice IV becomes queen of Egypt after temporarily dethroning her father, Ptolemy XII Auletes.
PS J:\windows>

 

Invoke-RestMethod http://numbersapi.com/random/date

This is what I saw when this was run

PS J:\windows> Invoke-RestMethod http://numbersapi.com/random/date
December 3rd is the day in 1982 that a soil sample is taken from Times Beach, Missouri that will be found to contain 300 times the safe level of dioxin.
PS J:\windows> Invoke-RestMethod http://numbersapi.com/random/date
March 22nd is the day in 1982 that NASA’s Space Shuttle Columbia, is launched from the Kennedy Space Center on its third mission, STS-3.
PS J:\windows>

 

Invoke-RestMethod http://numbersapi.com/random/math

This is what I saw when this was run

PS J:\windows> Invoke-RestMethod http://numbersapi.com/random/math
2622 is a value of n for which 7n and 8n together use each digit exactly once.
PS J:\windows> Invoke-RestMethod http://numbersapi.com/random/math
1352 is an hexagonal prism number.
PS J:\windows>

 

So as you can see an easy api to use, it does not require any keys and a lot of useful facts and information are returned.

 

Video