RAD Studio 10.4.1 is now available! Learn more. Looking for discounts? Visit our Special Offers page!
Delphi

Using the new REST Client library components to connect to Web & Cloud services (Part 2)

Author: Al M1426

This REST BaaS (Backend as a Service) client tutorial shows how to use the Embarcadero REST Library for accessing REST-based web services (REST stands for Representational State Transfer). The library is available for all platforms that are supported by Delphi. The REST Library framework focuses on JSON as the representation format. XML is not explicitly supported.

In Part 1, we discussed the sample “RESTDemos.exe” (built from sample project C:UsersPublicDocumentsRAD Studio12.0SamplesDelphiRESTDemo).

In this Part 2, we’ll walk through the steps to create a REST Client to use BaaS, using the new REST Client Components in Delphi XE5.

1. Create a new FireMonkey (FM) Desktop HD application.  Set Form.Caption = “XE5_REST_BasS_Client”. Save All.  Saved project as:  C:UsersembtDocumentsRAD StudioProjectsXE5_REST_BasS_Client

2. Drop “TRESTClient“, “TRESTRequest” and “TRESTResponse” components.

Notice that they are automatically connected together (RESTRequest has “Client” and “Response” properties).

3. In the Object Inspector:

a. For the RESTClient component, for the “Base-URL” enter “http://api.discogs.com/” into the “RESTClient1.BaseURL” property.

b. For the TRESTRequest component, for the “Resource-URI” enter “artist/{NAME}” into the “RESTRequest1.Resource” property.

Notice that “Params” property in the Structure View now contains “NAME” param.

Because of the special {} delimiters it’s a replaceable parameter, we can add the Parms.

4. Select “NAME” param in the Structure View, so it is selected in the Object Inspector.   In the Value property for RESTRequest1.Params[0], enter “Kelly Clarkson” as the “Artist-Name”.

So that’s all we need to do!  These three components are automatically hooked up by the designer.  Request references the Client and the Response component is for what we get back from the REST Provider.

5. Right-click on the “RESTRequest1” component in the form designer and select “Execute” to execute the request.

If the request was executed OK, we should see the “Response 200 – HTTP/1.1 200 OK” message:

Click OK to close it.

6.In the Object Inspector,  select “RESTResponse1” component and verify that the “Content” property contains JSON text with information about the artist.

7. To see the data, let’s use Live Bindings.  Right click the “RESTResponse1” component and select “Bind Visually”.

The three components have these default members.

8. In the Visual LiveBindings designer, right click on the “RESTResponse1.Content” property and select “Link to New Control”. Select TMemo from the “Bind to new Control” list.  Check “Add control label”.

Click OK.

9. Reposition and resize the Memo1 component.

10. In the Object Inspector, set the “MemoContent.WordWrap” property to “True”. The TMemo displays the RAW JSON results:

11. In the Visual LiveBindings designer,  right-click on the RESTRequest1 “Params.NAME” property, click on “Link to new control” and select “TEdit”

Click OK

12. Move “EditParamsNAME” control on the form out the memo, and more to the top of the form,  like this:

13. Add a “TButton” to the form.  In the Object Inspector, set Button1.Text = “Execute”.

14. Double-click on the button and in the OnClick event enter this line of code to execute the request:

RESTRequest1.Execute;

15. Run the project (F9), click on the button to see the response from the service.   RAW Response for Kelly Clarkson gets returned.

16. Change the artist name to something different (i.e. Michael Jackson), click again and observe different response displayed in the memo.

17. Stop the application.  We might be interested in displaying just a subset (some specific content returned) of the JSON response.

18. In the Visual LiveBindings designer change the “MemoContent” “Text” link from “RESTResponse1.Content” to “JSONValue” property.   Now we can drill down into the JSON Value.

19. On the Form, select the RESTResponse component.  In the Object Inspector, RESTResponse has a RootElement property.  JSON has a path, like “resp.artist.profile”.   This will go down the hierarchy of the JSON and give us the Profile information.  So this is more useful information, that just seeing RAW JSON returned.

In the Object Inspector, for the “RESTREsponse1” component, enter into its “RootElement” property “resp.artist.profile” and observe that the content in the memo has changed.

This ends this Part 2 tutorial showing how to create a Client using the new Delphi XE5 REST Library components to access BaaS.

In Part 3, we’ll discuss the “RESTDebugger.exe” app (built from C:Program Files (x86)EmbarcaderoRAD Studio12.0sourcedatarestrestdebugger) as a tool for making ad hoc requests.


Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial   Upgrade Today

   Free Delphi Community Edition   Free C++Builder Community Edition

Related posts
C++

What's New in the GetIt Package Manager - June 2020

C++

RAD Studio 10.4 Now Available, Learn More

C++NewsRAD Studio

Open for Business with 10.4! - May 2020 GM Update

C++DelphiNews

Embarcadero Open Source Project Sponsorship

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

IN THE ARTICLES