Tutorial: Using the REST Client Library to Access REST-based Web Services

From Appmethod Topics
Jump to: navigation, search

Go Up to Tutorials

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

Tip: This example requires an SSL encrypted connection. You can install the SSL library and copy the libeay32.dll and ssleay32.dll files to your system path.

Creating the Project

Create a new project.

  • For Object Pascal, choose File > New > FireMonkey Desktop Application - Object Pascal.
    • Select HD FireMonkey Application.
  • For C++, choose File > New > FireMonkey Desktop Application - C++.
    • Select HD FireMonkey Application.

Adding the REST Components

  1. Drop the TRESTClient, TRESTRequest, and TRESTResponse components on the form.
    Note: The REST components are automatically connected together. Otherwise, you should set the Client and Response properties of the TRESTRequest component to RESTClient1 and RESTResponse1, respectively.
  2. In the Object Inspector, set the BaseURL property of the TRESTClient to http://api.discogs.com/.
  3. Select the TRESTRequest component on the form and set the following properties:
    • In the Object Inspector, set the Resource property to artist/{NAME}.
    • In the Structure View, expand the Params node.
    • Select the NAME parameter.
    • Set the parameter value to Michael Jackson.
      Note: The Resource URI (artist/{NAME}) has an internal parameter, enclosed in curly braces. The internal parameter value automatically adds an URL segment parameter to TRESTRequest component.
  4. Right-click the TRESTRequest component and select Execute.
  5. Click the OK button.

Using the LiveBindings Designer

Open the LiveBindings Designer. The diagram with no connections is similar to the following image:


  1. Right-click the RESTResponse1.Content property.
  2. Select the Link to new control option.
  3. From the list that appears, select the TMemo control.
  4. Click the OK button.
  5. In the Object Inspector, set the following properties of the TMemo component:
    • Set the Align property to Client.
    • Set the WordWrap property to True.
      If you have called the Execute method of the TRESTRequest component, the TMemo should already display the RAW JSON results.
  6. Add a TPanel component on the form.
  7. Set the Align property of the TPanel to Top.
  8. In the LiveBindings Designer, right-click the Params.NAME property of the RESTRequest1.
  9. Select the Link to new control option.
  10. From the list that appears, select the TEdit control.
    Note: The Add control label check box is checked by default, so a TLabel is created for the TEdit control.
  11. Move the EditParamsNAME control to the top of the form.
  12. Add a TButton component on the form.
  13. Set the button's Text property to Execute.

Preparing your Application for Run Time

Adding Code to Display a Subset of JSON Response

  1. Add a TComboBox component on the panel and set the following properties:
    • In the Object Inspector, open the String List Editor for the Items property.
    • Add the Content and JSONValue items to the list.
    • Click the OK button to close the editor.
    • In the Object Inspector, set the ItemIndex property to 0.
  2. In the Form Designer, select the Button1 component.
  3. In the Object Inspector, double-click the OnClick event.
  4. Add the following code to this event handler:

Object Pascal:

procedure TForm10.Button1Click(Sender: TObject);
  if (ComboBox1.ItemIndex = 1) and (jValue is TJSONObject) then
    MemoContent.Text:= jValue.ToString
    MemoContent.Text:= RESTResponse1.Content;

The TJSONValue and TJSONObject classes are declared in the Data.DBXJSON unit, so you need to add Data.DBXJSON in the uses clause of your unit.


void __fastcall TForm10::Button1Click(TObject *Sender)
   TJSONValue *jValue;
   jValue = RESTResponse1->JSONValue;
   if ((ComboBox1->ItemIndex == 1) && (jValue != NULL)){
	MemoContent->Text = jValue->ToString();
	MemoContent->Text = RESTResponse1->Content;

The TJSONValue and TJSONObject classes are declared in the Data.DBXJSON library, so you need to add #include <Data.DBXJSON.hpp> in your header unit.

Running your Application

To run the application, press F9 or choose Run > Run. You can choose Content or JSONValue from the ComboBox control, change the TRESTRequestParameter value, and click the Execute button.<br\><br\> RESTTutorialRunTime2.png

See Also