Writing REST applications in Delphi is pretty simple with the advanced components we have available these days. The functionality encapsulated allows us to spend time on the business and user interface aspects of development rather than the nuances of connecting to an API and parsing JSON results. When starting a new REST app, I usually use the REST Debugger that comes in RAD Studio to test out an API before building the app itself. There are other REST API tools but this one has a great feature that saves me time building my Delphi app that no other tool has.
When getting set up to use a REST API in a Delphi app, there are several non-visual components that work together to call the API and store the result. I recently learned there are also components that parse the results into a dataset, making the task of using the JSON data much simpler.
REST Client Components
First, a TRESTClient is used to establish the connection to the API and execute a request; it contains the base URL and can be shared with several TRESTRequest components that specify which resources, or API endpoints, are called and the parameters passed to the REST server. These components also handle security, encoding, connection timeouts, and so forth, nicely packaged and ready to use.
The result of calling a REST server, if successful, returns a response in the
Content property of a TRESTResponse component. That's where I thought using components ended and coding began--capturing the JSON result into local variables and parsing through them is a rather tedious process.
Then I discovered the TRESTResponseDataSetAdapter and my life suddenly got a lot easier!
Automatic JSON Parsing
Typical REST servers respond with structured data in a JSON object. Using classes in System.JSON and related units works but takes awhile if you have to parse everything yourself. You used to have to do that but I just discovered that several versions ago, a huge boost to productivity snuck it's way onto the Delphi tool palette. Now, by simply adding a TRESTResponseDataSetAdapter to your app, hooking it your TRESTResponse to it and adding a dataset (such as a FireDAC memory table), it can parse the JSON result for you into a very convenient table of rows and columns! Even if your result set is not an array, there are still likely several elements and instead of parsing it yourself, just throw it into a one-row table with this great time-saving component.
So when was TRESTResponseDataSetAdapter introduced? I'm not sure but I saw it listed in the library documentation on Embarcadero's Docwiki as far back as Delphi 10 Seattle, although it was not mentioned in that version's What's New list.
While it's not terribly difficult to manually add these components to your form or data module, (I'd happily do this instead of writing and maintaining many lines of code to parse the JSON), there's an even easier way to get this set up using a nifty button in Embarcadero's REST Debugger.
A couple years back, I was watching a video about working with APIs in Delphi and using the REST Debugger to help test when suddenly the presenter clicked a button on the REST Debugger that had been there the whole time but I just had totally missed it: the Copy Components button! This puts component objects on the clipboard in the right format to enable you to switch to your Delphi project and just paste them onto a form or data module! I had to try this myself so opened it up, selected an API to call to see some results, switched to the Body tab of the REST Debugger to confirm, then clicked the Copy Components button, and pasted them on my Delphi project's data module. It created
TRESTResponse components for me, all hooked up ready to call the same API from my app. How convenient!
So now I started using this to quickly set up REST API components in my Delphi apps but was still tediously parsing the JSON until I discovered the
TResponseDataSetAdapter I mentioned above. Then just last week, I was using the REST Debugger's Tabular Data tab to view parsed JSON results and clicked the Copy Components button again; this time, when I pasted in Delphi, I got two additional components:
I went back and checked and sure enough, if you're on the Body tab of the REST Debugger, the Copy Components copies the three REST components to the clipboard but if you're on the Tabular Data tab, it copies five components--the three REST components along with the TResponseDataSetAdapter and TFDMemTable, all hooked up for you!
It's sometimes hard to keep up with all the cool new stuff in Delphi unless you buy every book, watch every video, and read every blog that comes out--anyone who says Delphi makes you write lots of code has some catching up to do!
I know about the Copy Components button for the three components. Thanks to you, now I know about the TResponseDataSetAdapter component in Delphi Berlin.
I knew about this component but for newer version of Delphi. Just wao