visit
You may use command-line cURL and send a request from the terminal. cURL’s disadvantage is the difficulty to switch between requests and sharing the responses with your teammates.
What about building UI on top of cURL? The most popular tool to deal with API requests is Postman. It’s quite powerful and has many great features. But very often you don’t want a bulky machine gun when a screwdriver is enough. I personally prefer , cause it’s much easier and basically does the same thing. It’s handy when it comes to exploratory testing, or when you want to share the requests with developers.
To show my favorite Insomnia features I’ll use a real web application called . Conduit is a simplistic clone of Medium. Conduit is implemented in many popular languages and frameworks. I will use Spring Boot backend and React frontend to demonstrate how it works. Let’s spin the testing environment on the local machine.
git clone //github.com/drakulavich/spring-boot-realworld-example-app.git
cd spring-boot-realworld-example-app docker-compose up -d
git clone //github.com/drakulavich/react-redux-realworld-example-app.git
docker-compose up -d
Base Environment can have default values and Sub Envs will override them. Usually, I add to sub envs user details and test stand URL. To make names consistent you may choose patterns like {env_name} with {user_name}
.
To reference the field from Environment, start typing _ (underscore symbol) and choose a variable from the context menu. The user is created. The next step would be sending a Login request to obtain the valid auth token. The token will be used for all the other requests under the user’s name.
It’s not sufficient to copy/paste the token for each request, that’s why Insomnia has Requests Chaining. Switch to Auth → Current User request and open Auth tab. Select Bearer Token authentication and start typing response word in the Token field. Insomnia will show you a popup menu. All we are interested in is the Response → Body Attribute. Click on the tag to specify details.
We need to specify the Request and filter out the value using JSONPath. If you see the correct value in Live Preview, it means everything is ready. All you have to do later is copy the tag and paste it into every request that requires the token. We touched only two Insomnia features: Environments and Requests Chaining. And look how powerful they are! You can improve your productivity even further by using hotkeys.
After installing, you will find new suggestions from Insomnia when you start typing faker word. After that Create Article request will produce a randomly generated body each time you click Send button. Cool! We can easily add several posts under different user names. It will be enough to test multiple use cases such as:
How to share the request with a developer? The easiest way is to open the context menu for the request and choose Copy as Curl. If you attach the request to the bug report, the developer will be grateful.
So, we have created users and added several blog posts. It’s time to explore the web app (). Imagine the situation when you are working with the web app, and you want to debug the request that the browser just sent. Open Developer Tools, switch to the Network tab, and select the request. Chrome allows copying the request as cURL, the same way we did in Insomnia.
Open Insomnia, create a new dummy request without any data. Then, insert the cURL from Chrome to the address bar. Insomnia will parse the request and show it with the body, headers, etc. I usually prefer to debug API in Insomnia, and this is how you can import the request from the Browser.
The last thing I’d like to demonstrate is response filtering with JSONPath. Let’s execute request Articles → All Articles. The response is quite heavy to read. Imagine we are interested only in one field from the collection of the articles. Use JSONPath to filter the output. For example, get only the favoritesCount field: