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

FireWind

Свой
Регистрация
2 Дек 2005
Сообщения
1,957
Реакции
1,203
Credits
4,034
How To Make A Simple REST Client In C++ And More
By Yilmaz Yoru July 7, 2021

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​

Для просмотра ссылки Войди или Зарегистрируйся is a connectivity method that allows to get and post data to create interactive applications that use Web Services. REST uses a subset of Для просмотра ссылки Войди или Зарегистрируйся. 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 Для просмотра ссылки Войди или Зарегистрируйся 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 Для просмотра ссылки Войди или Зарегистрируйся 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 Для просмотра ссылки Войди или Зарегистрируйся. It is highly recommended to understand the basics of Stack Exchange API and REST Debugger.

Для просмотра ссылки Войди или Зарегистрируйся 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 Для просмотра ссылки Войди или Зарегистрируйся, and also Trial Professional Version can be downloaded from Для просмотра ссылки Войди или Зарегистрируйся

What REST Components are available in RAD Studio?​

The Для просмотра ссылки Войди или Зарегистрируйся 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 (Для просмотра ссылки Войди или Зарегистрируйся)​

The RESTClient (Для просмотра ссылки Войди или Зарегистрируйся) 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.
1625727186369.png

REST Request (Для просмотра ссылки Войди или Зарегистрируйся)​

The RESTRequest (Для просмотра ссылки Войди или Зарегистрируйся) 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,
C++:
RESTRequest1->Client   = RESTClient1;
RESTRequest1->Response = RESTResponse1;
1625727217230.png

REST Response (Для просмотра ссылки Войди или Зарегистрируйся)​

The RESTResponse (Для просмотра ссылки Войди или Зарегистрируйся) 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.
1625727227203.png

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Для просмотра ссылки Войди или ЗарегистрируйсяДля просмотра ссылки Войди или Зарегистрируйся” )
    Double click to its Params and parameters of your web service ( i.e. for theДля просмотра ссылки Войди или Зарегистрируйся 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 Для просмотра ссылки Войди или Зарегистрируйся variable or to a component property as below

How do I connect to the ExchangeRatesAPI?​

Here is an example to connect Для просмотра ссылки Войди или Зарегистрируйся
Код:
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. Для просмотра ссылки Войди или Зарегистрируйся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 Для просмотра ссылки Войди или Зарегистрируйся and try it in C++ Builder, it is also possible in Delphi too.