Test the powerful 4D REST server with Postman

Automatically translated from English

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 a first introduction to the 4D robust REST server. You’ll see how to configure it as well as 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 in order for REST requests to be processed:

Create and configure the 4D Database

  • This steps 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 line of code is needed in order 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.

In order 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.

  1. Choose the method GET from the list of methods
  2. Copy the URL
  3. Click send
  4. Voilà:

As you can see, not a single line of code is needed in order 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. In order 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).

  1. Change the method to POST
  2. Insert the URL
  3. Click send
  4. Add your new task
  5. 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 the most enduring works of Hemingway: 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 verfiy that the entity has been deleted! It’s gone, isn’t it?

What’s next

As you can see, the 4D REST server is very powerful. 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. In upcoming blog posts, I’ll share a tip on authentication, then a full front end application written in Angular 7 with REST for web access to the 4D database.

Avatar

• Product Marketing Manager •

Intissar joined 4D in 2017 as a Product Marketing Manager. She works closely with the product, marketing, engineering, and technical support teams to highlight the ‘why’, the ‘how’, and the ‘what’ of new and updated features to different audiences. This close proximity allows her to craft messaging frameworks, and write in-depth content and code samples for the 4D blog and website.

After graduating with an engineer's degree in Computer Science from VINCI university, Intissar worked in several startups as a software engineer. Her hands-on experience includes software specification, design, and development, user training and support, and team management.