REST

A REST API (also known as RESTful API) is an application programming interface that conforms to the constraints of REST architectural style and allows for interaction with RESTful web services.

A Codejig REST API allows you to retrieve data from a data source, or to send data. The data is delivered via JSON format.

Request

The request block allows you to perform HTTP methods for RESTful services. That includes GET, POST, PUT, PATCH, DELETE, OPTIONS, HEAD.

The request has the following parameters:

url: an URL to the web service you want to access.

response: the return type of the data, either Map or a Transient data type you have created (see the examples).

body: the data your API sends to the client.

authorization: token or Basic auth REST blocks that include the login information.

parameters:  key-value REST block.

headers: key-value REST block.

Look in the examples on the bottom of the page for the detailed usage of request blocks together with Codejig data types.

Key param value

The key-value block is used in the request block to provide key-value information for the request.

You may change the param name.

The value is intended to be of a Text type.

In the example below, the {“name”: “John”, “surname”: “Smith”} data will be posted on the given URL.

Bearer Token

The bearer token is a cryptic string, usually generated by the server in response to a login request. The client must send this token in the Authorization header when making requests to protected resources:

The block accepts Text value as a parameter.

The token block may be passed as a request header or authorization parameter.

Basic auth

Basic auth block is the most straightforward technique for enforcing access controls to web resources because it does not require cookies, session identifiers, or login pages. Basic authentication uses standard fields in the HTTP header.

The block has two placeholders that must contain Text credentials: login and password in plain form.

Response type

Using a Map is a quick way to get a response to a request.

With the Map, you can parse JSON files with a simple structure (not nested). For instance:

{“name”: value, “surname”: value}

To parse a JSON with a complex structure, you may need to create and use a Transient data type. Imagine you have:
{  "header":"Location name and its coordinates",

   "markers":[

      {

         "name":"Rixos The Palm Dubai",

         "location":[

            25.1212,

            55.1535

         ]

      } ]

}

Then you would create a Transient type with a String field named header and a collection named markers. Inside the markers collection, you must include name and location String fields. After that, you need to set the System names as the key names in the JSON file.

You must set System names header, markers, name, and location in correspondence with those fields. Only then the REST blocks will automatically handle the correspondence between the keywords in a JSON and the data type fields you want to fill data in.

If you have created a type with correct System names that correspond to the JSON fields, you can use that type as a response type. Request blocks will translate the JSON data correctly.

The significant remark is that you cannot request a complicated JSON with a chaotic form in statically typed languages, like JAVA or CJ blocks.

 

Example: requesting JSON data in real life.

In the following example, we will show you how to transform data from the users’ example file into the Codejig directory data displayed on the Application.

The JSON file contains “id”, “name”, “surname”, and “password” keys repeating multiple times. We can represent them in the form of a table with four columns and multiple rows. Codejig maps data from the file into the type using System names. Therefore System names in the Transient type must correspond to the JSON keys.

Step 1: Create transient type with the same schema as in the JSON file.

  1. Create a Transient type (we named it Users transient).
  2. Add the following regular fields:

“id”: Integer (nullable)

“name”: String (System)

“surname”: String (System)

“password”: String (System)

  1. Add exactly the same System names as the key names in the JSON file.

Step 2: Create the directory type where you will display the requested data.

  1. Create directory data type (we named it Users directory).
  2. From ADD NEW FIELD ⇒ Define drag in a Collection. We use a collection field as the JSON data contains the keys repeated multiple times.
  3. In the pop-up window, for the Data type field, select a transient type you created before; click “Create”.

Step 3: Write a code to request a JSON.

  1. Create On Load server action in your Directory (Users directory in our case). The code from the action will be triggered when the page starts to load.
  2. Set the item.@User Transient collection to the request block. You can find the collection from your web page on the Data Panel ⇒ Variables on the right.
  3. For the request block, add a parameter response - collection. The @ sign indicates a collection.
  4. As an URL parameter, pass the following link:

https://www.codejig.com/scripts/example_data/users_data.json

  1. Pass a Users transient data type as @response type parameters.
  2. Save and Publish your application.