0% found this document useful (0 votes)
1 views5 pages

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

This tutorial provides a step-by-step guide on using the REST Client Library in RAD Studio to access REST-based web services, specifically focusing on JSON format. It covers creating a project, adding REST components, using the LiveBindings Designer, and preparing the application for runtime. The tutorial includes code examples for Delphi and C++Builder to execute requests and display JSON responses from the Songsterr web service.

Uploaded by

Paul Chen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views5 pages

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

This tutorial provides a step-by-step guide on using the REST Client Library in RAD Studio to access REST-based web services, specifically focusing on JSON format. It covers creating a project, adding REST components, using the LiveBindings Designer, and preparing the application for runtime. The tutorial includes code examples for Delphi and C++Builder to execute requests and display JSON responses from the Songsterr web service.

Uploaded by

Paul Chen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Tutorial: Using the REST Client Library to Access REST-based Web Ser... http://docwiki.embarcadero.com/RADStudio/Seattle/en/Tutorial:_Using_...

Show: Delphi C++


Display Preferences

Tutorial: Using the REST Client Library to Access


REST-based Web Services
From RAD Studio

Go Up to Database and LiveBindings Tutorials

Contents
1 Creating the Project
2 Adding the REST Components
3 Using the LiveBindings Designer
4 Preparing your Application for Run Time
4.1 Creating the Event Handler for the Execute Button
4.2 Running Your Application
5 See Also

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.

Creating the Project


Create a new project.

For Delphi, choose File > New > Multi-Device Application - Delphi > Blank Application.

For C++Builder, choose File > New > Multi-Device Application - CBuilderUI > Blank Application.

Adding the REST Components


1. Drop the TRESTClient, TRESTRequest, and TRESTResponse components on the form.
Tip: Alternatively, you can copy all these three components to the clipboard using the Copy
Components button of the REST Debugger Tool, and then paste them to the form. This helps you
speed up the work.
2. In the Object Inspector, set the BaseURL property of the TRESTClient to http://www.songsterr.com
/a/ra/.
3. Select the TRESTRequest component on the form and set the following properties:
In the Object Inspector, set the Resource property to songs.json.
In the Structure View, expand the Params node.
Right-click the Params node, and click Add item on the context menu.

1 of 5 12/27/2018, 10:24 AM
Tutorial: Using the REST Client Library to Access REST-based Web Ser... http://docwiki.embarcadero.com/RADStudio/Seattle/en/Tutorial:_Using_...

Under Params, click the newly added parameter 0-.


In the Object Inspector, set the parameter name to pattern, and the parameter value to Madonna.

4. Right-click the TRESTRequest component and select Execute.

5. Click the OK button.

Note: This scenario illustrates how to get a response from the Songsterr Web service in the JSON
(http://www.json.org) format. The Songsterr Web service also supports the xml and plist formats. For more
information, see http://www.songsterr.com/a/wa/api.

Using the LiveBindings Designer


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

2 of 5 12/27/2018, 10:24 AM
Tutorial: Using the REST Client Library to Access REST-based Web Ser... http://docwiki.embarcadero.com/RADStudio/Seattle/en/Tutorial:_Using_...

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 TextSettings/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. Drop a TPanel component on the form.
7. Set the Align property of TPanel to Top.

3 of 5 12/27/2018, 10:24 AM
Tutorial: Using the REST Client Library to Access REST-based Web Ser... http://docwiki.embarcadero.com/RADStudio/Seattle/en/Tutorial:_Using_...

8. In the LiveBindings Designer, right-click the Params.pattern property of the RESTClient1.


9. Select the Link to new control option.
10. From the list that appears, select the TEdit control, and click OK.
Note: The Add control label check box is checked by default, and a TLabel is created for the TEdit
control.
11. Move the Params.pattern control to the top of the form.
12. Add a TButton component on the form, and set the button's Text property to Execute.

Preparing your Application for Run Time


Creating the Event Handler for the Execute Button

1. In the Form Designer, select the Button1 component.


2. In the Object Inspector, double-click the OnClick event.
3. Add the following code to this event handler:

Delphi:

procedure TForm1.Button1Click(Sender: TObject);


var
jValue:TJSONValue;
begin
RESTRequest1.Execute;
jValue:=RESTResponse1.JSONValue;
MemoContent.Text:= jValue.ToString;
end;

Important: The TJSONValue class is declared in the System.JSON unit. Before compiling this
project, please add System.JSON to the uses clause of your unit.

C++Builder:

void __fastcall TForm1::Button1Click(TObject *Sender) {


TJSONValue *jValue;
RESTRequest1->Execute();
jValue = RESTResponse1->JSONValue;
MemoContent->Text = jValue->ToString();
}

Important: The TJSONValue class is declared in the System.JSON library. Before compiling this
project, please add #include <System.JSON.hpp> to the project header file.

Running Your Application

To run your application:

1. Press F9 or choose Run > Run.


2. Enter a pattern (such as Madonna) in the Param.pattern text box.
3. Click Execute to send your request to the Songsterr service.

4 of 5 12/27/2018, 10:24 AM
Tutorial: Using the REST Client Library to Access REST-based Web Ser... http://docwiki.embarcadero.com/RADStudio/Seattle/en/Tutorial:_Using_...

See Also
REST Client Library
REST Debugger
REST.RESTDemo Sample
Bind to new control
LiveBindings Designer

Retrieved from "http://docwiki.embarcadero.com/RADStudio/Seattle


/e/index.php?title=Tutorial:_Using_the_REST_Client_Library_to_Access_REST-based_Web_Services&
oldid=245440"

Categories: FMX Delphi C++ XE5

This page was last modified on 13 July 2015, at 22:39.


Help Feedback (QP, email)

5 of 5 12/27/2018, 10:24 AM

You might also like