Ditch Postman and save time with IntelliJ’s HTTP client

-

In this blog post, I explore a vital guide to IntelliJ’s HTTP client.

While setting up an application with REST endpoints, it is often the case that you want to call an endpoint and examine the received response. In more complex situations, you may want to use different methods, send headers, or include cookies. For this purpose, there are, of course, various applications you can use, such as Postman or Insomnia. But why switch between your IDE and another app when you can simply use these functionalities within IntelliJ itself?

IntelliJ Ultimate users have access to the HTTP client. This client has received a good update, as previously you had to configure the REST calls via a UI – which resulted in a lot of clicking – but now it’s possible to configure the REST calls via a text file. This is ideal, as these files are easy to exchange and can even be checked into version control.

Why IntelliJ’s HTTP client?

IntelliJ’s HTTP client seamlessly integrates into your development workflow, eliminating the need to switch between different applications. No longer do I find myself constantly switching between various applications. Here are some reasons why IntelliJ’s HTTP client might just become your preferred choice as well:

  1. Native Integration: When you already use IntelliJ as your primary IDE, using its HTTP client means you don’t have to install or switch to additional software.
  2. Code Autocomplete: You can leverage IntelliJ’s powerful autocomplete feature to quickly fill in endpoint URLs and headers to improve your productivity.
  3. Code Navigation: IntelliJ allows you to navigate through your HTTP client files just like any other code file, making it easy to find and organize your API calls within your project.

I’ve found that using IntelliJ for HTTP requests is a solid choice, and its user-friendly design makes it a great tool. Let’s explore some fundamental abilities that I like to use.

A simple GET call

So, how do you use it? First of all, you create a .http file, then you can use plain text to configure calls. We will dive into all the syntax later on. In this example, I’ll first demonstrate how easy it is to make a simple GET call:

GET https://api.example.com/cards

That’s it! And the beauty of this is that IntelliJ knows about the endpoints you are exposing in your project, so it also provides autocomplete for your URLs.

Once the call is executed, you’ll receive a response code and body directly within your IDE. One way to run this call is by clicking the green play button in the gutter, but we can also search for comments we have provided to run the call. All we have to do is add a comment above the call in the http file by using three hashes and then search for the comment in the Run Anything window (press control twice for Mac users).

### get all cards
GET https://api.example.com/cards

This is very helpful when you want to run a quick call without navigating to the .http file.

Crafting a call with headers

The HTTP client can also handle headers, request bodies, and variables. Let’s use a POST call to explore these features.

In this example, we’ve included headers specifying the content type as JSON and provided a JSON request body. IntelliJ provides warnings if the syntax of the body doesn’t match the specified content type, ensuring you catch errors early in the development process.

Additionally, IntelliJ supports variables, both predefined and custom, allowing for dynamic and reusable requests. There are a bunch of predefined variables to help you in generating test values like random.uuid or random.email.

Of course it is also possible to set your own variables. This is useful when you want to prepare data before a call or process it after. But you will also need it if you want to store environment related variables like host, key or password variables. For this purpose we create a separate .env.json file.

In this file you can set multiple environments, for now we only configure a dev environment. Then we can use this variable in our .http file by using the double curly brackets and selecting the environment in the ‘Run with’ drop down menu (in this case dev).

Harnessing the power of JavaScript

IntelliJ’s HTTP client goes beyond simple request crafting; it allows you to leverage JavaScript to run tests and manipulate data before making API calls. Here’s a glimpse of its capabilities:

In this example, we’re using JavaScript to validate the response status. IntelliJ provides default objects and methods like client.assert() and client.test() for writing tests, as well as access to response data such as headers and body. You can even set your own global variables using client.global.set() or request-specific variables using request.variables.set().

The above examples are designed to give you an idea of what is possible. But for inspiration to see what more is possible you can always browse the examples button in the right top corner when you have your http file open.

Conclusion

In conclusion, IntelliJ’s HTTP client offers a powerful and intuitive solution for API development and testing, seamlessly integrating into your development environment. With features like autocomplete, variable support, and JavaScript capabilities, it provides a comprehensive toolkit for crafting and testing API calls right from your IDE. Whether you’re a seasoned developer or just getting started, IntelliJ’s HTTP client is a valuable addition to your toolbox.