4D provides a powerful REST server that enables direct access to data stored in your 4D databases. This makes it possible, for example, to build an API to use with a modern front-end technology (e.g., Angular, React, etc.). In this blog post, we’ll provide the first introduction to the 4D robust REST server. You’ll see how to configure it and test the create, retrieve, update, and delete (CRUD) operations using the API testing tool, Postman.
Configure the REST server
NOTE: If you already know how to configure a REST server and 4D database, you can skip the next two sections.
To make use of the 4D REST server, you’ll need to configure it first (a single click to get up and running). Simply check the” Expose as REST server” option in the “Web/REST resource” page of the database settings for REST requests to be processed:
Create and configure the 4D Database
- This step assumes that you’ve already created a database and activated the Rest server. Go to your structure and create a new table [Tasks], with two attributes: Title (Alpha) and Complete (Boolean). By default, all tables are exposed in REST:
Important: table and field names must be JSON-compliant (no diacritical characters or spaces).
- Now, go ahead and create input and output forms. Enter some tasks in the Tasks table.
- From the Run menu, select Start Web Server (if it’s not already started), then select Test Web Server.
- Congratulations! You can now read and edit data within 4D using only REST requests.
How can you tell? Easy, open a web browser, and after the ADDRESS:PORT, insert “/rest“. (All 4D REST URL requests start with /rest).
For example, If you want to get all entities of the [Tasks] dataclass, you can proceed as follows:
Testing the API with Postman
The amazing thing about the 4D REST server is that the API provides the CRUD (and more!) operations … ready to go! Not a single code line is needed to create, read, update, or delete an entity. Everything is set up for you. As we all know, CRUD is the most important group of database operations since they’re the main functions users need to create and manage data.
To test this amazing API, we’ll use Postman (an excellent tool for testing RESTful APIs). Postman offers a sleek user interface to make HTML requests, such as GET, POST, PUT/UPDATE, DELETE, and various other request methods.
Note: Using Postman is straightforward, however, check out this video if you need help getting started.
GET the list of tasks
Once Postman is downloaded, launch it, and we’ll create our different requests. We’ll begin with the most obvious request: Retrieving the list of tasks (from our [Tasks] table). As stated earlier, to get all entities of the [Tasks] dataclass, be sure to insert /rest/NameOfTheDataclass after the ADDRESS:PORT.
- Choose the method GET from the list of methods
- Copy the URL
- Click send
As you can see, not a single line of code is needed to retrieve the list of tasks!
Create a new task
We can also add a new task to our dataclass … also without code. The API is already set for you!
To create a new entity, this URL will fire up a new post request: ADDRESS:PORT/rest/NameOfTheDataclass/?$method=update. To do so, we need to send a JSON request to the application. If you check the results from the GET request, you’ll get an idea of the keys needed to create a new task. So following this logic, we’ll only need the title and the complete fields; 4D takes care of the rest (key, timestamps, stamp, and ID).
- Change the method to POST
- Insert the URL
- Click send
- Add your new task
- Make sure that success = true!
Go back to the first tab (GET method), click Send, and verify that the task has been added. You can also go to your 4D output form to see if the new task has been added!
Update a task
To update an entity, we’ll use the same method that we used when creating the entity. $method=update allows you to update one or more entities in a single POST. To do so, you need to pass the __KEY and __STAMP parameters in the object along with any modified attributes.
In our example, we’re done reading one of Hemingway’s most enduring works: The Old Man and the Sea. Therefore, we need to change the complete status of the task to true. Easy:
Delete a task
Deleting an entity is also straightforward. In our example, we want to delete the task with ID = 3 (called “test”). No problem! Call the delete method and specify it using its ID: dataclass(ID)?$method=delete.
Go back to your 4D database and verify that the entity has been deleted! It’s gone, isn’t it?
As you can see, the 4D REST server is mighty. It provides a rich API beyond what we’ve just demonstrated. There’s much more that can be done in a short time. Check out the detailed documentation. I’ll share a tip on authentication in upcoming blog posts, then a full front-end application written in ReactJS with REST for web access to the 4D database.