As various web APIs become more widespread we keep getting more and more questions how to access this or that API from EasyMorph. We will be addressing this need with a number of new actions in one of the nearest releases. Until then, as a workaround it is possible to use the PowerShell action with a short script for sending web requests and parsing JSON responses. Here is an example:
$url = "http://free.currencyconverterapi.com/api/v5/convert?q=USD_CAD&compact=y"
(Invoke-WebRequest -Uri $url).Content | ConvertFrom-Json | Select -expand "USD_CAD" | Select -expand "val"
The example sends a GET request to a free web-service to receive a somewhat recent USD/CAD exchange rate. The response comes in as JSON that looks like {"USD_CAD":{"val":1.33933}}
. We’re using PowerShell’s command ConvertFrom-Json
to parse it and make it an object, which property val
is accessed using Select -expand
. The value of val
is captured back into EasyMorph.
Here is the full example. To make it a bit more interesting, the source and target currencies are defined as EasyMorph parameters and used in the script.
web-api-request.morph (1.6 KB)
1 Like
Hi
Thanks for sharing this.
What about if we need to send JSON data to another API rather than simple parameters?
For example something like:
request={
“PurchaseId”: “2fcd96b6-775d-46d1-bd86-bbaa3c6f540c”,
“ItemsToAdd”: [
{
“Id”: “f83759d6-c3c2-4f6b-af82-12683ef08216”,
“StockItemId”: “f3ca3a76-e25b-43c7-869c-05c2179886ad”,
“Qty”: 3,
“PackQuantity”: 4,
“PackSize”: 5,
“Cost”: 1.0,
“TaxRate”: 2.0
},
{
“Id”: “f83759d6-c3c2-4f6b-af82-12683ef08216”,
“StockItemId”: “f3ca3a76-e25b-43c7-869c-05c2179886ad”,
“Qty”: 3,
“PackQuantity”: 4,
“PackSize”: 5,
“Cost”: 1.0,
“TaxRate”: 2.0
}
],
“ItemsToUpdate”: [
{
“Id”: “3b6bc652-f15c-470b-82ce-fb37dd94735d”,
“PurchaseItemId”: “bbd9104d-adcb-46d6-956e-8e2baa509e01”,
“StockItemId”: “2ed79418-d0a9-4431-88b1-2d65745ad48c”,
“Qty”: 4,
“PackQuantity”: 5,
“PackSize”: 6,
“Cost”: 1.0,
“TaxRate”: 2.0
},
{
“Id”: “3b6bc652-f15c-470b-82ce-fb37dd94735d”,
“PurchaseItemId”: “bbd9104d-adcb-46d6-956e-8e2baa509e01”,
“StockItemId”: “2ed79418-d0a9-4431-88b1-2d65745ad48c”,
“Qty”: 4,
“PackQuantity”: 5,
“PackSize”: 6,
“Cost”: 1.0,
“TaxRate”: 2.0
}
],
“ItemsToDelete”: [
“d956f994-d200-4299-be38-6fcb3760dd21”,
“be760a2b-3e15-4fa3-9d4c-67d8208627df”
]
}
You can create a JSON object using expressions and actions (see example below), then use Invoke-WebRequest
to send it.
make-json.morph (4.6 KB)