Software Development

IntelliJ’s text based HTTP client

IntelliJ provides a HTTP client that is purely text based. While this might sound strange at the beginning it turns out that this is a very useful feature.

Getting started

First we need to create a file whose name ends with .http or .rest. For example: my-requests.http.

To issue a simple GET request we have to write down the request in our newly created file.

For example:

1
GET http://localhost:8080/products

IntelliJ now adds a small Run-Icon next to the line which allows you to execute the request.

If we want to POST a piece of JSON, we simply have to add a Content-Type header and the request body:

1
2
3
4
5
6
7
POST http://localhost:8080/products
Content-Type: application/json
 
{
  "name""My other Product",
  "description""hu?"
}

Please note that there has to be a blank line between headers and request body.

Of course IntelliJ has syntax highlighting and auto completion for writing down headers and JSON:

Multiple requests in the same file need to be separated with ###. For example:

01
02
03
04
05
06
07
08
09
10
11
GET http://localhost:8080/products
 
###
 
POST http://localhost:8080/products
Content-Type: application/json
 
{
  "name""My other Product",
  "description""hu?"
}

Using variables

With {{ .. }} we can add variables to our requests. Maybe we want to issue the same request against different environments. To support this, we can update our request with a host variable:

1
GET http://{{host}}/products

Next we need to define the {{host}} variable. For this we create a http-client.env.json file and add the following content:

1
2
3
4
5
6
7
8
{
  "development": {
    "host""http://localhost:8080"
  },
  "production": {
    "host""http://my-cool-api.com"
  }
}

This defines two environments: development and production. Both environments define the host variable with a different value.

When running the request, we can now choose the environment we want:

Share requests with your team

The simple text-based request definition allows easy sharing with your team. You can even check in request files into your version control system. Of course you do not want to check in passwords or API keys that might be needed for request execution. IntelliJ supports this with a separate private environment file (http-client.private.env.json). Like in the previous environment example, we can use this file to define variables.

For example:

1
2
3
4
5
{
  "dev": {
    "api-key""S3CR3T"
  }
}

To make sure no secrets are checked in, we can explicitly exclude this file from our version control system.

Published on Java Code Geeks with permission by Michael Scharhag, partner at our JCG program. See the original article here: IntelliJ’s text based HTTP client

Opinions expressed by Java Code Geeks contributors are their own.

Michael Scharhag

Michael Scharhag is a Java Developer, Blogger and technology enthusiast. Particularly interested in Java related technologies including Java EE, Spring, Groovy and Grails.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

3 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
nwillc
nwillc
4 years ago

Is this feature still a limited version in the community vs the commercial version? I seem to remember it was.

Vijay
Vijay
4 years ago
Reply to  nwillc

It’s only in ultimate version

Prasad
Prasad
4 years ago

What is best plugin Rest Client Plugin for Intelij Community editon.

Back to top button