C++DatabaseLanguage FeatureLearn C++

How To Make A Simple REST Client In C++ And More

What is REST? What is “RESTful”? How we can develop a simple REST client? How we can use JSON based free APIs with REST? Can we connect to JSON-based Web Services? In this post, we answer your questions and we will show you how you that can simply build a REST Client.

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.

The REST client component in the object inspector

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,

The REST request client in the object inspector

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.

Binding REST responses in the OI

REST Connection with REST Components

Easily a simple REST connection can be done by using these 3 REST components in C++ Builder.

  1. Create a new Multi-Device C++ Builder application, save all unit and project files to a folder
  2. Add a Button (TButton) and a Memo (TMemo) component to see all JSON data.
  3. Drag RESTClient (TRESTClient , RESTRequest (TRESTRequest) and RESTResponse (TRESTResponse) and arrange their positions to see their names.
  4. Click to RESTClient1, and on the Object Inspector window,
    set its BaseURL property to RESTFull web service ( i.e. for the ExhangeRatesAPIhttp://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)
  5. 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

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.

close

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.


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

Free C++Builder Community Edition

About author

33+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux and some other operating systems. Dr. Yilmaz Yoru was born in 1974, Eskisehir-Turkey. He graduated from the department of Mechanical Engineering of Eskisehir Osmangazi University in 1997. One year later he started to work in the same university as an assistant. He received his MSc and PhD degrees from the same department of the same university. He has married and he is a father of a son. Some of his interests are Programming, Thermodynamics, Fluid Mechanics and Artificial Intelligence. He also likes the graphical 2D & 3D design and high-end innovations.
Related posts
C++Introduction to C++Language FeatureLearn C++

How To Get A Directory Name From A Path String

C++Introduction to C++Language FeatureLearn C++

How To Delete A File On Windows In C++ Builder

C++Introduction to C++Language FeatureLearn C++

How To Delete A Directory In C++ Builder On Windows

C++C++11C++14C++17Introduction to C++Learn C++

How to Delete, Clear, Erase A C++ Vector Member On Windows

en_USEnglish