Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
247 views

Using The REST Client Library To Access REST-based Web Services

This document provides a tutorial on using the REST Client Library to access REST-based web services from Delphi or C++Builder applications. It demonstrates how to create a project, add REST components, bind controls to response properties, and write code to display different parts of the JSON response. The tutorial application allows the user to enter an artist name, click a button to execute the request, and view the raw or parsed JSON results.

Uploaded by

Arja'un Tea
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
247 views

Using The REST Client Library To Access REST-based Web Services

This document provides a tutorial on using the REST Client Library to access REST-based web services from Delphi or C++Builder applications. It demonstrates how to create a project, add REST components, bind controls to response properties, and write code to display different parts of the JSON response. The tutorial application allows the user to enter an artist name, click a button to execute the request, and view the raw or parsed JSON results.

Uploaded by

Arja'un Tea
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Tutorial: Using the REST Client Library to Access REST-

based Web Services


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 Delphi. 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
thelibeay32.dll and ssleay32.dll files to your system path.

Creating the Project

Create a new project.

 For Delphi, choose File > New > FireMonkey Desktop Application - Delphi.

 Select HD FireMonkey Application.

 For C++, choose File > New > FireMonkey Desktop Application - C++Builder.

 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
toRESTClient1 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 alClient.

 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 alTop.

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:

Delphi:

procedure TForm10.Button1Click(Sender: TObject);


var
jValue:TJSONValue;
begin
RESTRequest1.Execute;
jValue:=RESTResponse1.JSONValue;
if (ComboBox1.ItemIndex = 1) and (jValue is TJSONObject) then
MemoContent.Text:= jValue.ToString
else
MemoContent.Text:= RESTResponse1.Content;
end;

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.

C++:

void __fastcall TForm10::Button1Click(TObject *Sender)


{
TJSONValue *jValue;
RESTRequest1->Execute();
jValue = RESTResponse1->JSONValue;
if ((ComboBox1->ItemIndex == 1) && (jValue != NULL)){
MemoContent->Text = jValue->ToString();
}
else
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 theExecute button.

See Also

 REST Client Library

 REST Debugger

 RESTDemo Sample

 Bind to new control

 LiveBindings Designer

You might also like