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:
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:
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:
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:
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:
{ "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:
{ "dev": { "api-key": "S3CR3T" } }
To make sure no secrets are checked in, we can explicitly exclude this file from our version control system.
Leave a reply