Here you can get knowledge about how to send a GET request and read a JSON response in Business Central – AL. I made a simple real-time currency exchange rates viewing page. I used a free rest API to get currency exchange rates. Follow the below steps and AL code and try it yourself.
Steps:
- Register at currencyapi.com, and then you can get a free API key to get access to their REST API to retrieve currency exchange rates (In the free account, there are 300 free requests per month), or you can find another API:
- Create the table “Currency Exch. Rate API Buffer” to insert currency data that are coming from the API response.
- Create the “Realtime Currency Exch. Rates” page to view currency data inserted in the table “Currency Exch. Rate API Buffer”.
- Use HttpClient to send GET requests through the request URL to the API endpoint in AL programming. Here is the entire AL code:
table 50100 "Currency Exch. Rate API Buffer"
{
fields
{
field(1;"Currency Code"; Code[10]){}
field(2;"Exchange Rate"; Decimal)
{
DecimalPlaces = 2:6;
}
}
keys
{
key(Key1; "Currency Code")
{
Clustered = true;
}
}
}
page 50100 "Realtime Currency Exch. Rates"
{
PageType = List;
ApplicationArea = All;
UsageCategory = Lists;
SourceTable = "Currency Exch. Rate API Buffer";
SourceTableTemporary = true;
Editable = false;
layout
{
area(Content)
{
repeater(Control1)
{
field("Currency Code"; Rec."Currency Code")
{
ApplicationArea = All;
Style = Strong;
}
field("Exchange Rate"; Rec."Exchange Rate")
{
ApplicationArea = All;
Style = Unfavorable;
}
}
}
}
actions
{
area(Processing)
{
action(Refresh)
{
ApplicationArea = All;
Image = Refresh;
Promoted = true;
PromotedIsBig = true;
PromotedCategory = Process;
trigger OnAction()
begin
InsertCurrencyData();
end;
}
}
}
trigger OnOpenPage()
begin
InsertCurrencyData();
end;
local procedure GetCurrencyDataByAPI() JsonString: Text;
var
Client: HttpClient;
Response: HttpResponseMessage;
begin
//Sending the GET request through currency API and getting the response as JSON string..
if not client.Get('https://api.currencyapi.com/v3/latest?apikey=GF9w85SLtC7UaTYA49hVKBzFwjdhTHdGdCqAKDkY', Response)then
Error(RequestFailError);
Response.Content().ReadAs(JsonString);
end;
local procedure InsertCurrencyData()
var
JsonObj: JsonObject;
JsonString: Text;
JsonTokenVar: JsonToken;
Element: JsonToken;
begin
//Reading the JSON response string as a JSON object and getting values(Currency code and exchange rate)..
ClearRec();
JsonString := GetCurrencyDataByAPI();
if not jsonObj.ReadFrom(JsonString) then
Error(InvalidResponseError);
JsonTokenVar.ReadFrom(JsonString);
if JsonTokenVar.IsObject and JsonObj.Get('data',JsonTokenVar) then begin
JsonObj := JsonTokenVar.AsObject();
foreach Element in JsonObj.Values DO
InsertRec(GetJsonObjectValueFromToken('code',Element).AsText(),GetJsonObjectValueFromToken('value',Element).AsDecimal());
end;
end;
local procedure GetJsonObjectValueFromToken(Parameter: Text; JsonTokenVar: JsonToken): JsonValue;
begin
//Getting values of the parameters in the JSON object..
JsonTokenVar.AsObject().Get(Parameter,JsonTokenVar);
exit(JsonTokenVar.AsValue());
end;
local procedure ClearRec()
begin
Rec.Reset();
Rec.DeleteAll();
end;
local procedure InsertRec(CurrencyCode: Code[10]; ExchangeRate: Decimal)
begin
//Inserting currency data to the Rec.
Rec."Currency Code" := CurrencyCode;
Rec."Exchange Rate" := ExchangeRate;
Rec.Insert();
end;
var
RequestFailError: Label 'Unable to process the request through the API!';
InvalidResponseError: Label 'Invalid response!';
}
Code language: PHP (php)
After running the “Realtime Currency Exch. Rates” page It shows retrieved currency exchange rates through the API. Here is the output:
Visit the previous guide about Interfaces in AL programming.