API call using HttpClient-GET

Well, I loved my API QRcode trial and that is the inspiration behind this new API demo. I have been searching for a free API that could be useful in BC, but I could not find out one that fits with my level of expertise :P. But still, much appreciated, if anyone can suggest one.

If you are someone like me who was a bit too scared of touching .net and dlls in NAV, below post would be a good start:-

There are a couple of interesting blogs that explain quite a lot about API calls. A few are:-

https://andreilungu.com/

https://www.kauffmann.nl/category/webservices/

Back to my API experiment. I am using https://api.covid19api.com/summary api here. Please check https://covid19api.com/ for the documentation of API. Let’s hope, we will be out of this covid trouble in the near future. I know, nobody is going to use this API in BC. But you can always take a look at my code and get inspired 🙂 . I am just calling this API and inserting the response data into a table.

    procedure ImportData()
    var
        Client: HttpClient;
        Response: HttpResponseMessage;
        ResponseString: Text;
        JsonTokenData: JsonToken;
        JsonTokenData1: JsonToken;
        JsonObj: JsonObject;
        url: Text;
        JsonArr: JsonArray;
        jsToken: JsonToken;
        jsToken1: JsonToken;
        CoronaCases: Record "Corona Cases";

    begin
        url := 'https://api.covid19api.com/summary';
        Client.Get(url, Response);
        if not Response.IsSuccessStatusCode() then
            Error('Web service returned error:\\' +
              'Status code: %1\' +
              'Description: %2',
              Response.HttpStatusCode(),
              Response.ReasonPhrase());
        Response.Content.ReadAs(ResponseString);
        JsonTokenData.ReadFrom(ResponseString);
        JsonObj := JsonTokenData.AsObject();
        JsonObj.SelectToken('Countries', JsonTokenData1);
        JsonArr := JsonTokenData1.AsArray();

        CoronaCases.DeleteAll();
        CoronaCases.Init();
        foreach jsToken in JsonArr do begin
            clear(jsToken1);
            if jsToken.SelectToken('Country', jsToken1) then begin
                if jsToken1.IsValue then
                    CoronaCases.Country := copystr((jsToken1.AsValue().AsText()), 1, 30);
            end;
            clear(jsToken1);
            if jsToken.SelectToken('TotalConfirmed', jsToken1) then begin
                if jsToken1.IsValue then
                    CoronaCases."Total Confirmed" := jsToken1.AsValue().AsInteger();
            end;
            clear(jsToken1);
            if jsToken.SelectToken('TotalDeaths', jsToken1) then begin
                if jsToken1.IsValue then
                    CoronaCases."Total Deaths" := jsToken1.AsValue().AsInteger();
            end;
            clear(jsToken1);
            if jsToken.SelectToken('TotalRecovered', jsToken1) then begin
                if jsToken1.IsValue then
                    CoronaCases."Total Recovered" := jsToken1.AsValue().AsInteger();
            end;
            CoronaCases.Insert();
        end;
    end;

You can see my table, page and codeunit here – https://github.com/manjusree16/AL-Demos/tree/master/Covid-19%20API%20call

Oh, I forgot to show the output

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create your website with WordPress.com
Get started
%d bloggers like this: