Ken Maglio
Automation Solution Engineer My License Plate says it all "I-AU2M8"
RESTful API
GET POST PUT DELETE
PowerShell
and
Ken Maglio @kenmaglio
Thank you for joining us!
Monthly Meetings posted on MeetUp
Live Broadcasts and Recordings
Thank you so much!
Your Organizers:
Mike
Ken
A structured application that allows computers, not humans, to retrieve data or perform actions easily.
What is REST?
APIs started their life using XML to transfer data between systems. However, XML brings lots of overhead just for the syntax.
Method + Base-Uri + Endpoint + Parameters
http://netflixroulette.net/api/api.php?title=Attack%20on%20titan
Verb | Action | Example |
---|---|---|
GET | Retrieve Data | |
POST | New Data / Action | |
DELETE | Remove Data | |
PUT | Update Data |
Invoke-RestMethod -Uri $uri -Method "POST" -Body $json
Invoke-RestMethod -Uri $uri -Method "GET"
Invoke-RestMethod -Uri $uri -Method "DELETE"
Invoke-RestMethod -Uri $uri -Method "PUT" -Body $json
Expects HTML as a response
Returns Headers!!!!
Good for querying normal web pages
Expects JSON or XML as a response
Only returns data -- No Headers
Great for RESTful APIs -- go figure!
Booooooooo
Yeah!!!
Many ways to get API tokens
Simply chuck the token in as an URL parameter
You better be using HTTPS!!!
Used in some simpler APIs - where token is not securing, just identification
e.g. Google URL Shortener, Wolfram Alpha, etc.
$token = "eaeha0ef9awefawe8awe0f"
$url = "https://api.openweathermap.org/data/2.5/"
# Call API - using Token
$response = Invoke-RestMethod -Uri "$url/weather?q=London&token=$token" -Method Get
Typically authentication is passed in headers
Most commonly: "X-Auth-Header" or "Authentication"
$token = "eaeha0ef9awefawe8awe0f"
# Invoke-RestMessage -Headers takes a hash
$headers = @{"X-Auth-Header" = "$Token"}
$url = "https://api.openweathermap.org/data/2.5/"
# Call API - using Token
$response = Invoke-RestMethod -Uri "$url/weather?q=London"
-Headers $headers -Method Get
Note: Careful using herestrings. Hard to manipulate and dynamic JSON is rough.
(JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate. It is based on a subset of the JavaScript Programming Language.
Depth default = 3
Automatic conversion of special characters: < > \ ' &
$Json | ConvertTo-Json | Foreach-object { [System.Text.RegularExpressions.Regex]::Unescape($_) }
<?xml version="1.0" encoding="ISO-8859-1"?>
<document>
<patient>
<firstname>Test</firstname>
<lastname>Patient</lastname>
</patient>
<allergies>
<allergy>
<name>Wheat</name>
<severity>3</severity>
</allergy>
</allergies>
</document>
{
"patient": {
"firstname": "Test",
"lastname": "Patient"
},
"allergies": {
"allergy": {
"name": "Wheat",
"severity": "3"
}
}
}
218 Characters
109 Characters
With Netflix's official API no longer giving out new keys; they decided to do away with all of that and make this an Open API. You're free to query as much information as you'd like, just mind their bandwidth.
https://github.com/kenmaglio/presentations
APIs are great! We can get the data we need, or update a system.
However, they are the most powerful when you start chaining them together.
In other words, use calls from one API to feed into another API.
API calls into systems like:
WWT Operations recently completed an overhaul of our VM provisioning process.
This process uses API calls into various systems to now automatically provision any quantity of virtual machines.
All done by chaining APIs
Please leave comments or questions!
By Ken Maglio
Presentation on using REST API's in PowerShell for the STLPSUG on 9/21
Automation Solution Engineer My License Plate says it all "I-AU2M8"