본문 바로가기

개발(합니다)/TOOL

[intellij] 인텔리제이 .http로 postman 대체하는 방법

반응형

개발하다보면 REST를 보내기 위해 Postman이나 api tool을 이용하곤 합니다.

인텔리제이에서 .http를 이용해 REST를 보낼 수 있는 기능이 있어

간단한 API 요청을 보낼때는 기존에 사용하던 REST tool을 대체하려고 합니다.

장점으로는 모든 요청을 .http 파일로 관리되어 git을 통해 파일 관리가 가능합니다.

 

관련 정보는 아래 사이트에서 확인해볼 수 있습니다.

www.jetbrains.com/help/idea/http-client-in-product-code-editor.html

www.jetbrains.com/help/idea/exploring-http-syntax.html#comments-in-http-requests


1. 간단한 컨트롤러 및 패키지 구성

패키지 구성은 아래와 같습니다.

1-1. ToolHttp.java

package com.otrodevym.spring.base.common.intellijtool;

import org.springframework.web.bind.annotation.*;



@RequestMapping("/intellij")
@RestController
public class ToolHttp {

    @GetMapping("/http/test")
    public String getHttpTest() {
        return "hellow";
    }

    @PostMapping("/http/test")
    public String postHttpTest(@RequestBody String test) {
        return test;
    }
}

 

1-2. hello.http 작성

2. GET 요청 해보기

GET http://localhost:9090/intellij/http/test

첫째 줄에는 HTTP Method와 URL

둘째 줄에는 RequestHeader를 작성합니다.

3. POST 요청 해보기

3-1. 간단한 POST 요청

POST http://localhost:9090/intellij/http/test
Content-Type: application/json

"test"

첫째 줄에는 HTTP Method와 URL

둘째 줄에는 RequestHeader

한줄 띄우고 다음줄에 Body 입력

3-2. 파일로 POST 요청

첫째 줄에는 HTTP Method와 URL

둘째 줄에는 RequestHeader

한줄 띄우고 다음줄에 Body 입력에 '<' 기호를 이용하여 파일 선택

POST http://localhost:9090/intellij/http/test
Content-Type: application/json

< ./post.json

파일 생성

4. Request Header 관련 및 기타

4-1. 인증

Authorization에 인증 토큰을 입력하면 인증이 가능합니다.

### 개발 정상
GET http://localhost:9090/intellij/http/test
Authorization: DEV 

4-2. 쿠키

### 쿠키
POST http://localhost:9090/intellij/http/test
Cookie: user=test

4-3. 메서드 구분

### 를 기준으로 메서드를 구분합니다.

GET http://localhost:9090/intellij/http/test

###

POST http://localhost:9090/intellij/http/test
Content-Type: application/json

"test"

###

4-4. 로그

요청을 하면 로그를 파일 단위로 남기게 되며 파일 위치는 /.idea/httpRequests/ 아래에 있습니다.

 

4-5. Controller에서 자동 생성

아이콘을 클릭하면 Http Request를 바로 실행하거나 수정할 수 있습니다.

4-6. <와 > 사용

< request body에 요청을 넣는 기호이며 json 파일 형태로 많이 사용

> response 결과 파일을 읽어오는 기호이며 js 파일 형태로 많이 사용

4-7. Dynamic 변수

$$uuid: UUID 
$$timestamp: Unix Timestamp 
$$randomInt: 0에서 1000 사이의 랜덤 숫자

4-8. 환경변수 설정 사용

{{host}} 형태로 이중 블릿으로 감싸서 사용합니다.

프로젝트이 기본적인 환경변수는

rest-client-env.json이거나 http-client.env.json 형태의 파일명을 사용합니다.

{
  "local" : {
    "host": "localhost",
    "user": "local"
  },

  "dev" : {
    "host": "localhost.dev",
    "user": "dev"
  }
}

 

아래와 같은 파일 형태를 가지고 있습니다.

환경설정이 추가되어 local과 dev가 표시됩니다.

dev와 lcoal로 실행한 결과입니다.

반응형