The built-in IntelliJ HTTP Client is very useful for testing HTTP requests and responses. We can use it to test for example a REST API that works with JSON data. If an endpoint expects a JSON payload we can specify the payload in our HTTP Client request file. But if we have a lot of endpoints and large payload the request file can get big and messy. Instead of having the payload in the request file directly we can specify an external JSON file with the payload and use it for a request body. We must use the < operator and give the name of the file with our JSON payload. The IntelliJ HTTP Client will read the contents of that file and use it as the request body. The payload may also contain (dynamic) variables and those variables will be replaced with correct values when the request is executed.

In the following example we have a POST request with a JSON payload from an external file. The payload in the file contains variables. We use an endpoint from the test API at that expects a JSON payload. The response will contain the payload with request payload where all variables are replaced in the json property.

### POST to /anything with payload from external file
Content-Type: application/json

< ./sample-payload.json

# Content of sample-playload.json:
#  "id": "{{$random.uuid}}",
#  "username": "{{username}}",

# Content of http-client.env.json with value for variable username:
#  "test": {
#    "username": "mrhaki"
#  }

> {%
    client.test("Response is OK", function () {
        client.assert(response.status === 200);
    client.test("Response has username set", function () {
        client.assert(response.body.json.username === "mrhaki");
    client.test("Response has id with value", function () {
        const regexExp = /^[0-9a-f]{8}\b-[0-9a-f]{4}\b-[0-9a-f]{4}\b-[0-9a-f]{4}\b-[0-9a-f]{12}$/gi;

Written with IntelliJ IDEA 2023.2.4.