Skip to main content
Dat 3. semester
Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Back to homepage

Using http files with rest

Using HTTP Files with REST in IntelliJ IDEA

Introduction

HTTP files are a feature in IntelliJ IDEA that allows you to create and execute HTTP requests directly from your IDE. This can be particularly useful for testing REST APIs without needing to switch to a separate tool like Postman or curl. In this guide, we will explore how to create and use HTTP files to interact with RESTful services.

  1. Creating an HTTP File

    • In IntelliJ IDEA, right-click on your project or a specific directory and select New > HTTP Request.
    • Name your file with a .http extension (e.g., api_requests.http).
  2. Writing HTTP Requests

    • Inside the HTTP file, you can write your HTTP requests using a simple syntax. For example:
      ### Get all users
      GET http://localhost:8080/api/users
      Accept: application/json
      
      ### Get a specific user by ID
      GET http://localhost:8080/api/users/1
      Accept: application/json
      
      ### Create a new user
      POST http://localhost:8080/api/users
      Content-Type: application/json
      
      {
        "name": "John Doe",
        "age": 30,
        "gender": "male",
        "email": "john.doe@example.com"
      }
      
         ### Update an existing user
         PUT http://localhost:8080/api/users/1
         Content-Type: application/json
      
         {
           "name": "John Doe Updated",
           "age": 31,
           "gender": "male",
           "email": "john.doe.updated@example.com"
         }
      
         ### Delete a user
         DELETE http://localhost:8080/api/users/1
         Accept: application/json
      
  3. Executing HTTP Requests

    • To execute a request, place your cursor on the request line (e.g., GET http://localhost:8080/api/users) and click the green play button that appears on the left side of the editor, or use the shortcut Ctrl + Enter (Windows/Linux) or Cmd + Enter (Mac).
    • The response will be displayed in a separate panel below the request, showing the status code, headers, and body of the response.
  4. Using Variables

    • You can define variables in your HTTP file to make your requests more dynamic. For example:
      @baseUrl = http://localhost:8080
      @userId = 1    
      
      Then you can use these variables in your requests:
      GET {{baseUrl}}/api/users/{{userId}}
      
  5. Organizing Requests

    • You can organize your requests using comments and sections. Use ### to create a section header, which helps in grouping related requests together for better readability.
  6. Using Environment Variables

    • IntelliJ IDEA allows you to define environment variables that can be used across multiple HTTP files. You can create an environment file (e.g., env.json) and reference it in your HTTP file:

    { “dev”: { “url”: “localhost:7070/api” }, “prod”: { “url”: “https://bugelhartmann.dk/api/" } }

  • Then, in your HTTP file, you can use these variables as follows:
    GET {{baseUrl}}/api/users/{{userId}}