Discussion

Most web pages that return useful data provide
this information with the intention that it will only ever be displayed
by a web browser. Extracting this information is always difficult,
although Parse and Analyze a Web Page from the Internet usually makes the solution simpler than
straight text manipulation.

When a web page is designed to be consumed by other programs or
scripts, it is usually called a web service or
web API. Web services are the more fully featured
of the two. They rely on a technology called SOAP (Simple Object Access
Protocol), and mimic traditional programming APIs that support rigid
structures, standardized calling behavior, and strongly typed objects.
Connect to a Web Service
demonstrates how to interact with web services from PowerShell.

While much less structured, web APIs tend to follow some similar
basic design philosophies—primarily URL structures, standard HTTP
methods (GET/POST), and data types (JSON/XML). These loosely defined
design philosophies are usually grouped under the term
REST (Representational State Transfer), making
REST API the term most commonly used for non-SOAP
web services.

While still designed to be consumed by programs or scripts, REST
APIs have a much less rigid structure. Because of their simplicity, they
have become the dominant form of web service on the Internet.

The Invoke-RestMethod cmdlet
forms the basis of how you interact with REST APIs from PowerShell. It
acts much like the Invoke-WebRequest
cmdlet in that it lets you invoke standard HTTP operations against URLs:
GET, PUT, POST,
and more. Unlike Invoke-WebRequest,
though, Invoke-RestMethod assumes
that the data returned from the website is designed to be consumed by a
program. Depending on the data returned by the web service (XML or
JSON), it automatically interprets the returned data and converts it
into PowerShell objects.

Note

If this interpretation is incorrect for a website or REST API,
you can always use the Invoke-WebRequest cmdlet directly.

As another example of interacting with REST APIs, Example 12-8 demonstrates using the StackOverflow
API to find the accepted answer for the PowerShell questions matching
your search term.

Example 12-8. Searching StackOverflow for answers to a PowerShell
question