Working with APIs

APIs have been around for some time, as has the concept of REST. You will know that RESTful endpoints created with Swagger are everywhere, and not only development but also operations people need to interact with APIs.

An Application Programming Interface (API) is a way for developers to interface with, for example, the operating system, as is possible with the Win32 API.
Representational State Transfer (REST) describes the architectural principle that developers can follow to create interoperable web services.

Whether it is a monitoring system that you want to grab logs off, a web interface to a build system, or the Graph API of one of the big IT businesses, PowerShell is ideal for working with APIs (read: RESTful endpoints). Especially services delivering JSON data make it very easy to interact with. In this chapter, we will explore how to interact with APIs. JSON stands for JavaScript Object Notation and describes a way to represent complex objects as lightweight strings.

The following code sample shows a PowerShell hashtable and a JSON string next to each other. Notice the similarities, such as the way keys are added, and the differences, such as the colon as an assignment operator:

# Hashtable
@{
PropertyName = 'Value'
ListProperty = @(
'ListEntry1'
'ListEntry2'
)
} | convertto-json -dept 100 | clip

# JSON
{
"ListProperty": [
"ListEntry1",
"ListEntry2"
],
"PropertyName": "Value"
}

We will explore how to create our own simple API with nothing but PowerShell, and use this to interact with.

Read the docs

It is essential to read the API documentation for the API you are going to implement if you don't want to fiddle around with the developer tools in your browser.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset