Overview
AdvantageBuilder allows your macros to call modern REST Web APIs using PowerShell or JavaScriptV8. This Quick Start guide shows how to perform the most common operations:
- GET — retrieve data
- POST — send data
- PUT — update data
- DELETE — remove data
- Send JSON payloads
- Parse JSON responses
- Use authentication headers
JScript.NET is not included in this Quick Start. If your macro is written in JScript, you can call a JavaScriptV8 or PowerShell macro to perform Web API operations and return the data.
PowerShell Examples
PowerShell provides Invoke-RestMethod and Invoke-WebRequest, which make
Web API calls simple and automatically convert JSON responses into objects.
GET (JSON Response)
try {
$url = "https://api.example.com/items"
$response = Invoke-RestMethod -Uri $url -Method Get
$response # Already converted from JSON
}
catch {
alert "Error: $($_.Exception.Message)"
}
GET with Headers (Authentication)
$token = "YOUR_BEARER_TOKEN"
$response = Invoke-RestMethod `
-Uri "https://api.example.com/items" `
-Method Get `
-Headers @{ Authorization = "Bearer $token" }
POST (Send JSON)
$body = @{
name = "New Item"
price = 19.99
} | ConvertTo-Json
$response = Invoke-RestMethod `
-Uri "https://api.example.com/items" `
-Method Post `
-Body $body `
-ContentType "application/json"
PUT (Update Resource)
$body = @{ price = 24.99 } | ConvertTo-Json
Invoke-RestMethod `
-Uri "https://api.example.com/items/123" `
-Method Put `
-Body $body `
-ContentType "application/json"
DELETE
Invoke-RestMethod -Uri "https://api.example.com/items/123" -Method Delete
JavaScriptV8 Examples
JavaScriptV8 can call Web APIs using System.Net.Http.HttpClient. Responses are strings,
so JSON must be parsed manually using JSON.parse().
GET (JSON Response)
var HttpClient = clr.System.Net.Http.HttpClient;
var client = new HttpClient();
try {
var response = client.GetStringAsync("https://api.example.com/items").Result;
var data = JSON.parse(response);
write(data);
}
catch (ex) {
alert("Error: " + ex.Message);
}
GET with Headers (Authentication)
var HttpClient = clr.System.Net.Http.HttpClient;
var client = new HttpClient();
client.DefaultRequestHeaders.Authorization =
new clr.System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", "YOUR_BEARER_TOKEN");
var response = client.GetStringAsync("https://api.example.com/items").Result;
var data = JSON.parse(response);
write(data);
POST (Send JSON)
var HttpClient = clr.System.Net.Http.HttpClient;
var StringContent = clr.System.Net.Http.StringContent;
var Encoding = clr.System.Text.Encoding;
var client = new HttpClient();
var json = JSON.stringify({ name: "New Item", price: 19.99 });
var content = new StringContent(json, Encoding.UTF8, "application/json");
var response = client.PostAsync("https://api.example.com/items", content).Result;
var data = JSON.parse(response.Content.ReadAsStringAsync().Result);
write(data);
PUT (Update Resource)
var HttpClient = clr.System.Net.Http.HttpClient;
var StringContent = clr.System.Net.Http.StringContent;
var Encoding = clr.System.Text.Encoding;
var client = new HttpClient();
var json = JSON.stringify({ price: 24.99 });
var content = new StringContent(json, Encoding.UTF8, "application/json");
client.PutAsync("https://api.example.com/items/123", content).Result;
DELETE
var HttpClient = clr.System.Net.Http.HttpClient;
var client = new HttpClient();
client.DeleteAsync("https://api.example.com/items/123").Result;
Putting It All Together
Use PowerShell or JavaScriptV8 to call REST Web APIs, send JSON payloads, authenticate using headers, and parse JSON responses. These examples cover the most common operations used in modern automation and integration scenarios.