With the addition of the “PowerShell” transformation it’s become relatively easy to call various REST APIs from EasyMorph.
Below is a sample PowerShell script that obtains a list of categories from this Community forum, together with the number of posts in each category:
$headers = @{}
$response = Invoke-RestMethod -Method GET -Headers $headers -Uri https://community.easymorph.com/categories.json -ContentType "application/json; charset=utf-8"
foreach($category in $response.category_list.categories)
{
Write-Output ($category.name + ", " + $category.post_count + " posts")
}
Note that Invoke-RestMethod
automatically parses the obtained JSON and creates necessary properties in $response
. Here is the structure of categories.json:
{
"category_list": {
"can_create_category": true,
"can_create_topic": true,
"draft": null,
"draft_key": "new_topic",
"draft_sequence": 32,
"categories": [
{
"id": 1,
"name": "General Q&A",
"color": "B3B5B4",
"text_color": "FFFFFF",
"slug": "uncategorized",
"topic_count": 138,
"post_count": 547,
"position": 0,
...
Here is a result:
As an exercise, you can try obtaining the number of topics (not posts) for each category.
Read more:
Invoke-RestMethod (docs.microsoft.com)
UPDATE
Added a sample project:
rest-api-request.morph (1.3 KB)
UPDATE 1/23/2019
We’ve added a few actions to deal with REST APIs without scripting in PowerShell. See the tutorial here: Tutorial: Web requests