Creating APIs is a trend that is increasingly on the rise. Developing our server to make the information and resources of our system available for various environments helps us to keep our projects more organized. However, to develop an API, it is necessary to test the code created. In some situations, this can be a problem.
When we are listing data, we can access our API directly through the browser, from the client side. But if we need to test the creation of a new record via the POST method or the deletion of data via the DELETE method, we may encounter some difficulties. How can we perform these tests?
Have you ever created an HTML form tag to test sending data to a server? This can be quite tedious and not very productive, right? To address these issues, we have tools at our disposal that help us in testing REST APIs. One of these tools is Insomnia. It is very simple but very powerful.
In this article, we will perform its installation and see how to use it to test an API, along with additional resources.
Installation
The official Insomnia website is insomnia.rest.
We will download the “Insomnia Core” version, aimed at API testing. We will make use of the downloads page.
Mac
To install Insomnia on Mac, we can use Homebrew:
brew cask install insomnia
Linux
We can install on Linux via APT:
sudo apt-get update
sudo apt-get install insomnia
Windows
We can use Chocolatey to install Insomnia on Windows. To do this, we use the following command:
choco install insomnia-rest-api-client
If you prefer to install the conventional way, you can select the .exe file and download it. When you run it, Insomnia will be installed and started on your computer.
Testing an API
Let's test a cities API written in JavaScript, which will be available on our local server (localhost), on port 3000. We will test the CRUD routes for these cities.
Listing Cities
To make our first request, we will press the button with a plus (+) symbol and then click on “New Request”
We need to give a name to our request. We can name it whatever we want. In our case, let's call it “API LIST”:
The panel on our right will open. In it, we can specify the URL of our API at the top, next to the method, which in this case is “GET”. The URL of our API to list the cities is http://localhost:3000/cities:
After specifying the API address, just click on “Send”. Then, the panel further to the right will return our data:
Excellent! Our data was returned correctly. We might think: “This is good, but I could have the same result accessing the API through the browser.” This is true. However, using Insomnia allows us to keep our tests well organized and even more efficient. Additionally, when analyzing the next method, we will see some other advantages to using Insomnia that are simply indispensable.
Creating a City
The next HTTP method we will test is POST. To do this, we will repeat the process of clicking the new request button and then specifying a name for it. In this case, we will name it “API CREATE”. Additionally, in this same window we will change the method to “POST” and choose “Form URL Encoded” as the body of our request.
The URL will be the same, only the method of the request will change. Additionally, we will have the option to specify the fields we want to send to the API. Since we chose the form field option, this part will involve data with an identifier and a value:
When we click on “Send”, we will see the following result on the right panel:
Our data has been inserted, very good! Imagine doing this test by creating an HTML form. It would be much more work. With Insomnia, it has become much simpler, hasn't it?
Editing a City
The next method we will test is PUT. It is used to update all the information of a specific city. Another option would be the PATCH method, which is used to update only part of the record, not the entire entry.
To use the PUT method, we will repeat the process followed before with POST, changing only the name of the request to “API UPDATE”. We will also use the form body in this request:
The URL will contain the ID of the city we want to change and we will also be able to send the data for that update:
We will see the following result when we send the request:
Now our data has been updated.
Deleting a City
The last method we will test is the DELETE method. We will name this request “API DELETE”:
This request will not have a body. We will provide only the ID of the city we want to delete. In this example, we will delete the record with ID 2, which will be specified in the URL:
After sending the request, we see the following result:
We see that this route is also working, and we realize how simple it is to test our API with Insomnia. However, there are some additional features that can further facilitate our work.
Some improvements in our tests
Environment Variables
We use the server http://localhost:3000 as the basis for our tests. However, imagine that we need to change the address of this server to perform new tests. Or if we need to test our API in production. In this case, we would need to change the address of this server in all the requests we have already created. This could generate quite a repetitive and tedious job, wouldn't it?
To simplify this process, Insomnia has environment variables that can be reused. To access them, just click on “No Environment” > “Manage Environments” below the main Insomnia menu:
A new window will open. In it, we can see an area to add a JSON code. In it, we can define our environment variables. For example, we will define that our base URL will be localhost. We will use the following code for that:
{
"hcode_url": "http://localhost:3000"
}
The next step is to replace the string http://localhost:3000 with the variable hcode_url in our requests. To do this, simply type the variable name, and Insomnia will suggest it in the requests:
In the image above, we added the variable to the GET route, but we can do this in all requests. Now, if we need to change the base of our API, we simply change this information in one location and that change will reflect in all requests.
Folders
Another feature of Insomnia is folders. They allow us to group our requests into “namespaces,” preventing our tests from becoming disorganized as they increase.
To create a folder, just click on the button with a plus (+) symbol and then select “New Folder”:
We will name this folder “DEV” since the requests we created are aimed at development:
Now, just drag the requests we created into this folder:
Now, much more organized. This way, if we need to create specific requests for production or for other projects, they will not conflict with the development requests.
In this article, we had an introduction to Insomnia. We saw how simple it is to install and use, but also how it brings us various resources to facilitate our testing of REST-based servers. We hope you enjoyed this article and that you can use Insomnia in the development of your Web applications.
Taking advantage of the moment, we invite you to check out the courses from Hcode. If you want to stand out in the digital world, improve your programming skills, web development, or create your own applications and sites, our courses are the perfect choice. Access: www.hcode.com.br/cursos and discover how to boost your career with the knowledge and expertise that our qualified instructors can offer. Don't miss the opportunity to become an expert with Hcode!
See you in the next article :)