About EasyMorph Tutorials & Examples Web-help

Tip: How to call a REST API from EasyMorph


#1

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)


Importing JSON files