From Clomosy Docs

(Created page with "In Clomosy, the TclRest component is a component that facilitates interaction with RESTful services for data transfer. REST (Representational State Transfer) is a popular architecture for data transfer between web-based services. With the TclRest component in Clomosy, you can easily communicate with such services and exchange data. Using the TclRest component, you can make requests to a RESTful service (GET, POST, etc.), receive responses, and process these responses....")
 
No edit summary
Line 31: Line 31:


'''Example:'''<br>
'''Example:'''<br>
:'''Base Syntax'''
 
:'''TRObject Syntax'''
   var
   var
     Form1 : TclForm;  
     Form1 : TclForm;  
Line 37: Line 38:
     Button1 : TCLButton;
     Button1 : TCLButton;
    
    
   procedure GetPostMethod;
   void GetPostMethod;
   begin
   {
     clRest.Execute;
     clRest.Execute;
     ShowMessage(clRest.Response);
     ShowMessage(clRest.Response);
   end;
   }
    
    
   begin
   {
     Form1 := TclForm.Create(Self);
     Form1 = TclForm.Create(Self);
      
      
     Button1 := Form1.AddNewButton(Form1,'Button1', 'Click');
     Button1 = Form1.AddNewButton(Form1,'Button1', 'Click');
     Form1.AddNewEvent(Button1, tbeOnClick, 'GetPostMethod');
     Form1.AddNewEvent(Button1, tbeOnClick, 'GetPostMethod');
      
      
     clRest:=TCLRest.Create;
     clRest=TCLRest.Create;
      
      
     // Post
     // Post
     clRest.BaseURL := 'https://dummyjson.com/auth/login';
     clRest.BaseURL = 'https://dummyjson.com/auth/login';
     clRest.Accept := 'application/json';
     clRest.Accept = 'application/json';
     clRest.Method := rmPOST;
     clRest.Method = rmPOST;
     clRest.AddBody('{"username":"kminchelle","password":"0lelplR"}','application/json');
     clRest.AddBody('{"username":"kminchelle","password":"0lelplR"}','application/json');
      
      
Line 61: Line 62:
     /*
     /*
     // Get
     // Get
     clRest.BaseURL := 'https://dummyjson.com/auth/me';
     clRest.BaseURL = 'https://dummyjson.com/auth/me';
     clRest.Accept := 'application/json';
     clRest.Accept = 'application/json';
     clRest.Method := rmGET;
     clRest.Method = rmGET;
     clRest.AddHeader('Authorization','Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MTUsInVzZXJuYW1lIjoia21pbmNoZWxsZSIsImVtYWlsIjoia21pbmNoZWxsZUBxcS5jb20iLCJmaXJzdE5hbWUiOiJKZWFubmUiLCJsYXN0TmFtZSI6IkhhbHZvcnNvbiIsImdlbmRlciI6ImZlbWFsZSIsImltYWdlIjoiaHR0cHM6Ly9yb2JvaGFzaC5vcmcvSmVhbm5lLnBuZz9zZXQ9c2V0NCIsImlhdCI6MTcxMTIwOTAwMSwiZXhwIjoxNzExMjEyNjAxfQ.F_ZCpi2qdv97grmWiT3h7HcT1prRJasQXjUR4Nk1yo8');
     clRest.AddHeader('Authorization','Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MTUsInVzZXJuYW1lIjoia21pbmNoZWxsZSIsImVtYWlsIjoia21pbmNoZWxsZUBxcS5jb20iLCJmaXJzdE5hbWUiOiJKZWFubmUiLCJsYXN0TmFtZSI6IkhhbHZvcnNvbiIsImdlbmRlciI6ImZlbWFsZSIsImltYWdlIjoiaHR0cHM6Ly9yb2JvaGFzaC5vcmcvSmVhbm5lLnBuZz9zZXQ9c2V0NCIsImlhdCI6MTcxMTIwOTAwMSwiZXhwIjoxNzExMjEyNjAxfQ.F_ZCpi2qdv97grmWiT3h7HcT1prRJasQXjUR4Nk1yo8');
     */
     */
     Form1.Run;
     Form1.Run;
   end;
   }


:'''TRObject Syntax'''
:'''Base Syntax'''
   var
   var
     Form1 : TclForm;  
     Form1 : TclForm;  
Line 75: Line 76:
     Button1 : TCLButton;
     Button1 : TCLButton;
    
    
   void GetPostMethod;
   procedure GetPostMethod;
   {
   begin
     clRest.Execute;
     clRest.Execute;
     ShowMessage(clRest.Response);
     ShowMessage(clRest.Response);
   }
   end;
    
    
   {
   begin
     Form1 = TclForm.Create(Self);
     Form1 := TclForm.Create(Self);
      
      
     Button1 = Form1.AddNewButton(Form1,'Button1', 'Click');
     Button1 := Form1.AddNewButton(Form1,'Button1', 'Click');
     Form1.AddNewEvent(Button1, tbeOnClick, 'GetPostMethod');
     Form1.AddNewEvent(Button1, tbeOnClick, 'GetPostMethod');
      
      
     clRest=TCLRest.Create;
     clRest:=TCLRest.Create;
      
      
     // Post
     // Post
     clRest.BaseURL = 'https://dummyjson.com/auth/login';
     clRest.BaseURL := 'https://dummyjson.com/auth/login';
     clRest.Accept = 'application/json';
     clRest.Accept := 'application/json';
     clRest.Method = rmPOST;
     clRest.Method := rmPOST;
     clRest.AddBody('{"username":"kminchelle","password":"0lelplR"}','application/json');
     clRest.AddBody('{"username":"kminchelle","password":"0lelplR"}','application/json');
      
      
Line 99: Line 100:
     /*
     /*
     // Get
     // Get
     clRest.BaseURL = 'https://dummyjson.com/auth/me';
     clRest.BaseURL := 'https://dummyjson.com/auth/me';
     clRest.Accept = 'application/json';
     clRest.Accept := 'application/json';
     clRest.Method = rmGET;
     clRest.Method := rmGET;
     clRest.AddHeader('Authorization','Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MTUsInVzZXJuYW1lIjoia21pbmNoZWxsZSIsImVtYWlsIjoia21pbmNoZWxsZUBxcS5jb20iLCJmaXJzdE5hbWUiOiJKZWFubmUiLCJsYXN0TmFtZSI6IkhhbHZvcnNvbiIsImdlbmRlciI6ImZlbWFsZSIsImltYWdlIjoiaHR0cHM6Ly9yb2JvaGFzaC5vcmcvSmVhbm5lLnBuZz9zZXQ9c2V0NCIsImlhdCI6MTcxMTIwOTAwMSwiZXhwIjoxNzExMjEyNjAxfQ.F_ZCpi2qdv97grmWiT3h7HcT1prRJasQXjUR4Nk1yo8');
     clRest.AddHeader('Authorization','Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MTUsInVzZXJuYW1lIjoia21pbmNoZWxsZSIsImVtYWlsIjoia21pbmNoZWxsZUBxcS5jb20iLCJmaXJzdE5hbWUiOiJKZWFubmUiLCJsYXN0TmFtZSI6IkhhbHZvcnNvbiIsImdlbmRlciI6ImZlbWFsZSIsImltYWdlIjoiaHR0cHM6Ly9yb2JvaGFzaC5vcmcvSmVhbm5lLnBuZz9zZXQ9c2V0NCIsImlhdCI6MTcxMTIwOTAwMSwiZXhwIjoxNzExMjEyNjAxfQ.F_ZCpi2qdv97grmWiT3h7HcT1prRJasQXjUR4Nk1yo8');
     */
     */
     Form1.Run;
     Form1.Run;
   }
   end;

Revision as of 11:58, 22 August 2024

In Clomosy, the TclRest component is a component that facilitates interaction with RESTful services for data transfer. REST (Representational State Transfer) is a popular architecture for data transfer between web-based services. With the TclRest component in Clomosy, you can easily communicate with such services and exchange data.

Using the TclRest component, you can make requests to a RESTful service (GET, POST, etc.), receive responses, and process these responses.


Feature Use of Definition
TclRest clRest : TclRest; An object of the TclRest class is defined.
Create clRest=TclRest.Create; A new TclRest object is created on the form.
BaseURL clRest.BaseURL = 'https://dummyjson.com/auth/login'; The URL address to access is given.
Accept clRest.Accept = 'application/json'; Specifies the media type the server will accept in the response. It is indicated that data will be received in JSON format in the example.

The Accept property also defaults to sending values in JSON format.

Method clRest.Method = rmPOST; //rmGET

Specifies the type of HTTP request to be made. If you want to add new data to a resource or update an existing one with a POST request, you use rmPOST. If you want to retrieve the data of a resource with a GET request, you use rmGET.

AddBody clRest.AddBody('{"username":"kminchelle","password":"0lelplR"}','application/json'); Adds the provided data to the body of the specified request. This body forms the part where the data is sent in requests like POST. The data specified in the request is transmitted to the server through this body. It takes two parameters. The first parameter specifies the data to be sent in the POST request body, and the second parameter indicates the media type of the sent data.
AddHeader clRest.AddHeader('Authorization','Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MTUsInVzZXJuYW1lIjoia21pbmNoZWxsZSIsImVtYWlsIjoia21'); Adds the specified header to the HTTP request. It takes two parameters: the first parameter is the header name, and the second parameter informs the server about the media type of the sent data.

In the example, the "Authorization" header is used to carry authentication credentials. The value of the 'Authorization' header in the second parameter represents a JWT (JSON Web Token). This token is a verified and secured piece of data authenticated by the server for user identification.

Execute clRest.Execute; When this method is executed, the TclRest component performs the specified request and receives the response from the server.
Response ShowMessage(clRest.Response); You can access the details and content of the response from the server.

Example:

TRObject Syntax
 var
   Form1 : TclForm; 
   clRest : TCLRest;
   Button1 : TCLButton;
 
 void GetPostMethod;
 {
   clRest.Execute;
   ShowMessage(clRest.Response);
 }
 
 {
   Form1 = TclForm.Create(Self);
   
   Button1 = Form1.AddNewButton(Form1,'Button1', 'Click');
   Form1.AddNewEvent(Button1, tbeOnClick, 'GetPostMethod');
   
   clRest=TCLRest.Create;
   
   // Post
   clRest.BaseURL = 'https://dummyjson.com/auth/login';
   clRest.Accept = 'application/json';
   clRest.Method = rmPOST;
   clRest.AddBody('{"username":"kminchelle","password":"0lelplR"}','application/json');
   
   //clRest.AddHeader('Content-Type','application/json');
   
   /*
   // Get
   clRest.BaseURL = 'https://dummyjson.com/auth/me';
   clRest.Accept = 'application/json';
   clRest.Method = rmGET;
   clRest.AddHeader('Authorization','Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MTUsInVzZXJuYW1lIjoia21pbmNoZWxsZSIsImVtYWlsIjoia21pbmNoZWxsZUBxcS5jb20iLCJmaXJzdE5hbWUiOiJKZWFubmUiLCJsYXN0TmFtZSI6IkhhbHZvcnNvbiIsImdlbmRlciI6ImZlbWFsZSIsImltYWdlIjoiaHR0cHM6Ly9yb2JvaGFzaC5vcmcvSmVhbm5lLnBuZz9zZXQ9c2V0NCIsImlhdCI6MTcxMTIwOTAwMSwiZXhwIjoxNzExMjEyNjAxfQ.F_ZCpi2qdv97grmWiT3h7HcT1prRJasQXjUR4Nk1yo8');
   */
   Form1.Run;
 }
Base Syntax
 var
   Form1 : TclForm; 
   clRest : TCLRest;
   Button1 : TCLButton;
 
 procedure GetPostMethod;
 begin
   clRest.Execute;
   ShowMessage(clRest.Response);
 end;
 
 begin
   Form1 := TclForm.Create(Self);
   
   Button1 := Form1.AddNewButton(Form1,'Button1', 'Click');
   Form1.AddNewEvent(Button1, tbeOnClick, 'GetPostMethod');
   
   clRest:=TCLRest.Create;
   
   // Post
   clRest.BaseURL := 'https://dummyjson.com/auth/login';
   clRest.Accept := 'application/json';
   clRest.Method := rmPOST;
   clRest.AddBody('{"username":"kminchelle","password":"0lelplR"}','application/json');
   
   //clRest.AddHeader('Content-Type','application/json');
   
   /*
   // Get
   clRest.BaseURL := 'https://dummyjson.com/auth/me';
   clRest.Accept := 'application/json';
   clRest.Method := rmGET;
   clRest.AddHeader('Authorization','Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MTUsInVzZXJuYW1lIjoia21pbmNoZWxsZSIsImVtYWlsIjoia21pbmNoZWxsZUBxcS5jb20iLCJmaXJzdE5hbWUiOiJKZWFubmUiLCJsYXN0TmFtZSI6IkhhbHZvcnNvbiIsImdlbmRlciI6ImZlbWFsZSIsImltYWdlIjoiaHR0cHM6Ly9yb2JvaGFzaC5vcmcvSmVhbm5lLnBuZz9zZXQ9c2V0NCIsImlhdCI6MTcxMTIwOTAwMSwiZXhwIjoxNzExMjEyNjAxfQ.F_ZCpi2qdv97grmWiT3h7HcT1prRJasQXjUR4Nk1yo8');
   */
   Form1.Run;
 end;