• Blog
  • How a REST API call works in Delphi

How a REST API call works in Delphi

How to use the REST Debugger utility

Publish date:
Discover more of what matters to you

Overview

One of the most effective ways to create integrations for our software is to use the API (Application Programming Interface) structure.

APIs facilitate communication between a user and an application. But at the same time, users don’t need to know the details of the development process and the structure of the system that they will use. REST provides some specific criteria that are aimed at standardization and ensuring more flexible communication between different platforms. 

When an API meets the standards suggested by the REST architectural style, it can be described as a RESTful API.

In Delphi, REST API has some peculiarities in its functioning. Let’s have a look at them. To do it, we can build an example for Delphi rest-server consumption.

Delphi: rest server example of consumption

We will use a free apilayer that is available at: https://github.com/apilayer/restcountries.  

To open the REST Debugger, we should go to Tools -> Rest Debugger.

We have to choose which method we are going to use, and pass the URL with its parameters. 

In the case of this API, this is the URL: https://restcountries.com/v3.1

To filter all content, we need to use this URL: https://restcountries.com/v3.1/all

To filter a specific word, we should use this URL: https://restcountries.com/v3.1/name/word

*Word is the word to be filtered. 

In this case, we will use the “united” parameter to filter all parts that contain that word.

After entering the information, we have to click on Send Request.

Rest Debugger Menu

After sending the request and receiving the data in the REST Debugger, we can use the Copy Components button to make a snapshot of the configuration for the REST Client Library component which is required for building an application.

The next step is to insert it into your Delphi application. You can use your API either in VCL or FMX (Desktop or Mobile). In this REST API Delphi example, we will insert an edit button, DBGrid, and the data source to connect our grid. Let’s wire them up and execute the RESTRequest at design time to get a preview of the data:

Components of Rest Debugger

The code for filtering is very simple:

12345678
Procedure TFormExampleRest.btnSearchClick(Sender: TObject);
begin
if editSearch.Text = '' then
RestRequest1.Resource := 'all'
else
RestRequest1.Resource := 'Name/' + editSearch.Text;
RestRequest1.Execute;
end;

Reverse GeoCoding with the Position Stack API

In our second Delphi REST example, we will use the free version of a paid API, which can be found at: https://positionstack.com/

This API works the following way: it offers the service of geocoding, cartography, and many other possibilities. In our Delphi API REST example, we will use direct geocoding. We will send the address to receive latitude, longitude, and the information we get via the REST Debugger. To get the response we should configure the JSON root using the data element:

Rest Debugger Menu

We have to send two parameters to receive the request. One of them is a developer key, which can be obtained from the website: https://positionstack.com (You can select a free version). The second parameter is the address from which we want to get the information.

Just as we have done in the first case, we will copy the components and insert them into the Delphi application. In this situation, the application can display the data at runtime. In our example, we will get the latitude and longitude of the city that we have indicated:

Result of Rest Request

Conclusion

With the use of a REST API, Delphi developers who build mobile or desktop applications can leverage a row of benefits. In Delphi, REST API is helpful for performing integrations and speeding up the building and implementation of innovative solutions.

We are experts in Delphi software migration.

Contact us for a free consultation

Subscribe to our newsletter and get amazing content right in your inbox.

This field is required
This field is required Invalid email address
By submitting data, I agree to the Privacy Policy

Thank you for subscribing!
See you soon... in your inbox!

confirm your subscription, make sure to check your promotions/spam folder

Subscribe to our newsletter and get amazing content right in your inbox.

You can unsubscribe from the newsletter at any time

This field is required
This field is required Invalid email address

You're almost there...

A confirmation was sent to your email

confirm your subscription, make sure to check
your promotions/spam folder