Please enable JavaScript to view this site.

Codolex

Navigation: Activities

Rest operation

Scroll Prev Top Next More

The Rest operation activity can be used for a multitude of rest calls to send or retrieve data

Uses System.Net.HttpClientComponent.TNetHTTPClient

 

RestPorperties

Activity poperties

 

begin

var Response: string;

var Client := TNetHTTPClient.Create(nil);

try

  var HttpResponse := Client.GET('http://worldtimeapi.org/api/timezone/Europe/Amsterdam', nil, []);

  if (HttpResponse.StatusCode > 199) and (HttpResponse.StatusCode < 300) then

     Response := HttpResponse.ContentAsString

  else

    raise ENetException.CreateFmt('%d %s%s%s', [HttpResponse.StatusCode, HttpResponse.StatusText, sLineBreak, HttpResponse.ContentAsString]);

finally

   Client.Free;

end;

end;

Resulting code

 

The example shows a simple GET call to the WorldTimeAPI, but the activity offers a lot more options, let's go over them one by one.

Click here for an extensive explanation of this activity on our YouTube channel.

 

Operation

The HTTP Method that should be used in the call.

Possible options:

GET to retrieve information,

POST to send new information

DELETE to delete information stored elsewhere

PUT To update information stored elsewhere

PATCH to partially update information stored elsewhere.

 

Content-type

The type of content for the current call, in get calls this would be the content that the client can accept, in post calls this would be the type of content included in the request.

The list of possible options

 

URL

The URL to call, this can be a string, variable or expression, in the parameter section is explained how to use a string with parameters.

 

Body

The content of the current request this can be a string, variable or expression.

 

Parameters

A parameter can be added with a name and value.

There are 4 different types of parameters that can be used at the moment

 

1. Header

Adds a TNetHeader to the call.

 

2. Body (not allowed in GET Methods)

Adds a form data field to the request

 

3. Query

Adds a query paramter to the URL of the request

 

4. URL-Segment

Repaleces the par '{param name}' in the url with the value of the param.

 

e.g.

clip0067

'http://worldtimeapi.org/api/timezone/{area}/Amsterdam' ->

var HttpResponse := Client.GET('http://worldtimeapi.org/api/timezone/' + 'Europa' + '/Amsterdam', nil, []);

 

Authentication

Option to include basic auth into the request with username and password.

 

Timeout

The maximum amount of time the rest call should wait for respond in milliseconds (Default 60000)

 

Connection

The secure protocols option of the request. Multiple options are possible.

Possible options: SSL v2, SSL v3, TLS v1.0, TLS v1.1, TLS v1.2, TLS v1.3

 

Pagination

Many API's that handle a lot of data use pagination. a part of the result given, with an indicator for more results.

To help you with retrieving data from these kind of API's Codolex offers the pagination tab. There are two ways to handle pagination, with record/rows numbers, and with page numbers.

 

Loop until end of pages/records can be turned on to retrieve all data in one activity. Leave this option off when retrieving only one page, or when retrieving multiple pages manualy for partial retrieval.

 

Pagination

 

1. Records/rows

Request param for page size is the amount of records you want to receive from the API in one page. The first field is an editor for the name is has to have in the request, and the second for the value.

Request param for row/record number is the starting point from where the records should be returned, so if you already have the records somewhere, and you only want the new records, you can provide the amount of records you already have to receive new records. To receive all records, you should fill a 0 for the value. The first field is an editor for the name is has to have in the request, and the second for the value.

Response param for the next number is the name of the response header that contains the next records or total number. Provide this value when looping until the end of pages/records, so the code knows when to stop looping.

Return the response param in could be filled in with an integer variable. this variable is used to put the number or the next page or total records in. this could provide useful information when looping manually for partial retrieval.

 

2. Pages

The parameters for pages are more or less the same, but keep in mind that in this case its not the amount of records that is being worked with, but the amount of pages.

 

Logging

The logging tab can be used to set a log function that gets called when the request gets called.

To set the function, create a unit with the following class procedure

 

class procedure LogRestAction(const Client: TNetHTTPClient; const URL: string; const Method: string; const Content: TStream; const Headers: TNetHeaders);

units to use for this function are: System.Classes,  System.Net.URLClient,  System.Net.HttpClientComponent;

 

In this function you can use the parameters to write information to a log file. For example, at what time the url gets called.

 

Given the name of the unit RestActivity.Logger and the class TRestLogger, providing the function would look something like this:

RestActivityLogging

 

Response

EntityResult

1. Content as string value.

The raw content of the HttpResult is parsed as string value in the result value

 

2. HttpResponse entity

 

Its also possible to receive the result as an HttpResponse Entity. This entity is added as plugin datasource entity by default in codolex.

This entity can be used to receive information about the rest result like 'Status code' and 'http headers'

 

RecieveFromEntity

 

3. JSON value

The content as string can also be directly parsed into a JSON Value. This value can then be used in the JSON Activities to receive values.

This also has the option for a collection result, this option is mandatory when using pagination.

 

4. Selected entity

When you recieve a JSON from the rest call as response, and the only thing thats needed from the JSON is a (list of an) entity, you can also directly parse it into the entity.

This also has the option for a collection result, this option is mandatory when using pagination.

 

© by GDK Software