In this post, you’ll learn what REST is and what “RESTful” means. How can we create a simple REST client? How can we use free JSON-based APIs with REST? Is it possible to connect to JSON-based Web Services? By learning how to simply build a REST Client, it will help you to build C++ applications with the use of a C++ IDE.
Table of Contents
Everything you need to know about REST
REST (Representational State Transfer) is a connectivity method that allows to get and post data to create interactive applications that use Web Services. REST uses a subset of HTTP. A Web service that uses this REST data transfer architecture is called a RESTful. This web service must provide its web resources in a text form and this service also allows contents to be read and modified with a stateless protocol and a predefined set of operations. REST is a new modern data transfer architecture that allows interoperability between the computer systems on the Internet that provide these services. REST is Backend as a Service (BaaS) can be used in REST Server or REST Client applications, it is an alternative to SOAP to access a Web service. client tutorial shows how to use the REST Client Library for accessing REST-based web services (REST stands for Representational State Transfer).
C++ Builder makes working with REST very easy
One of the useful benefits of C++ Builder it supports developing REST Client and Server applications, it has REST Components and it has a REST Debugger that allows you to easily connect to JSON-based APIs as in Stack Exchange API.
In C++ and Delphi, the REST library is available for all platforms that are supported by both of them. The REST Library framework focuses on JSON as the representation format. XML is not explicitly supported. C++ Builder has REST-based connectivity as a Client or as a Server that provides you to do REST connections by the REST components that come with RAD Studio officially.
Stack Exchange runs on REST and is waiting for you
Before the example, there is a video about Stack Exchange API by Jim McKeeth recently added here. It is highly recommended to understand the basics of Stack Exchange API and REST Debugger.
C++ Builder comes with RAD Studio IDE and it is the easiest and fastest C and C++ IDE & Compiler for building simple or professional applications on the Windows, macOS, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. There is a free C++ Builder Community Edition for students, beginners, and startups, can be downloaded from here, and also Trial Professional Version can be downloaded from here
What REST Components are available in RAD Studio?
The Embarcadero REST Library is a framework 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 Embarcadero REST Library framework focuses on JSON as the representation format. XML is not explicitly supported.
Here are the main REST Client Components comes with RAD Studio;
REST Client (TRESTClient)
The RESTClient (REST.Client.TRESTClient) is the component that actually executes a request to a service. TRESTClient manages the HTTP connection to the service, deals with HTTP headers and proxy servers, and receives the response data. Authenticators, parameters can be attached to the client for a convenient way of adding authentication to a request.
REST Request (TRESTRequest)
The RESTRequest (REST.Client.TRESTRequest) holds all parameters and settings that form the actual HTTP request to the service. When connected to a client-component, it can be executed (even at design time). One of the most important properties of the request is the ‘Resource’. This value forms the previously mentioned ‘second half’ of the full request-url and defines the actual action that is executed on the service. The request also defines the HTTP methods (‘get’, ‘post’, ‘put’, ‘delete’) that are used for its execution.
TRestRequest component automatically sets it’s Client and Response properties as below, if you have more than one Client or Response components then you can manually add as below,
1 2 3 4 |
RESTRequest1->Client = RESTClient1; RESTRequest1->Response = RESTResponse1; |
REST Response (TRESTResponse)
The RESTResponse (REST.Client.TRESTResponse) holds all the returned data from the service. The data includes the HTTP status code, error messages (if any exist), and of course the returned JSON data. A response object can be created on the fly by the client while executing a request, or the response object can be created beforehand and then connected to a client before executing a request.
The response-data can be accessed using one of the ‘Content’, ‘JSONValue’, or ‘RAWbytes’ property.
REST Connection with REST Components
Easily a simple REST connection can be done by using these 3 REST components in C++ Builder.
- Create a new Multi-Device C++ Builder application, save all unit and project files to a folder
- Add a Button (TButton) and a Memo (TMemo) component to see all JSON data.
- Drag RESTClient (TRESTClient , RESTRequest (TRESTRequest) and RESTResponse (TRESTResponse) and arrange their positions to see their names.
- Click to RESTClient1, and on the Object Inspector window,
set its BaseURL property to RESTFull web service ( i.e. for the ExhangeRatesAPI “http://api.exchangeratesapi.io/v1” )
Double click to its Params and parameters of your web service ( i.e. for the ExhangeRatesAPI Endpoint with the Value=latest and access_key with the Value = YOUR API KEY parameters) - Now we can connect to any REST Service with their appropriate parameters and we can get the response message from this server to a TJSONValue variable or to a component property as below
How do I connect to the ExchangeRatesAPI?
Here is an example to connect ExhangeRatesAPI
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
void __fastcall TForm1::Button1Click(TObject *Sender) { //RESTClient1->BaseURL = "http://api.exchangeratesapi.io/v1"; //RESTClient1->AddParameter( "EndPoint", "latest", TRESTRequestParameterKind::pkGETorPOST); //RESTClient1->AddParameter( "access_key", "062b3c167bdd65bf8f0e84169f85d8abd", TRESTRequestParameterKind::pkGETorPOST); RESTRequest1->Resource = "latest"; RESTRequest1->Method = TRESTRequestMethod::rmGET; RESTRequest1->Response = RESTResponse1; RESTRequest1->Response->ContentType = "application/json"; RESTRequest1->Response->ContentEncoding="UTF-8"; RESTRequest1->Execute(); TJSONValue *jValue = RESTResponse1->JSONValue; Memo1->Lines->Add(jValue->ToString()); } |
If you still have problems with setting components, or if you have connection problems to your REST Web Service, you can also use REST Debugger to test REST Requests (Gets, Posts, …) and it is very easy to automatically set up these REST components with their parameters. This example may help you about how to use REST Debugger.
If you want to add live exchange rates features to your applications free, you can sign up to Exchangerates API, it can be registered from exchangeratesapi.io and try it in C++ Builder, it is also possible in Delphi too.