intellij http client

MyeongJae Lee·2024년 10월 20일
1

개발도구

목록 보기
1/1

공식문서를 보고 자주 사용할만한 것들을 간단하게 정리한 글입니다. 자세한 내용은 공식문서를 참고해주세요.

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

POSTMAN 보다 뭐가 나은데?

  1. intellij로 개발하니까 intellij로 테스트하면 편하다.
  2. 동료와 개발한 API에 대한 요청/응답을 공유할 수 있다.
  3. 제공하는 API가 다양하다.(postman도 있을지도?)

사실 2번이 크다고 생각합니다. 요청과 응답의 가시성이 좋고, 직접 실행도 해볼수 있기 때문이죠.

추가적으로 http test를 할 수도 있습니다.

https://blog.jetbrains.com/idea/2022/12/http-client-cli-run-requests-and-tests-on-ci/
github action
https://github.com/marketplace/actions/intellij-http-client-action


http client 사용을 위한 기본 개념

ref: https://www.jetbrains.com/help/idea/http-client-in-product-code-editor.html#using-response-handler-scripts

request & response scripts

pre-request scripts

> 또는 > {% %}로 시작

< scripts/my-script.js

POST https://example.org/pets/{{petName}}
< {%
	request.variables.set("petName", "Bella")
%}

POST https://example.org/pets/{{petName}}

response handler

< 또는 < {% %}로 시작

  • request와 괄호 방향이 다릅니다!
GET host/api/test

> scripts/my-script.js
GET host/api/test

> {% 
	// Response Handler Script ...
%}

Variables

ref: https://www.jetbrains.com/help/idea/exploring-http-syntax.html#using_request_vars

In-place variables

  • @var = value
  • 변수 적용 범위: 같은 파일(같은 파일 내의 요청에 적용)
@host = localhost:8080

GET {{host}}/api

In-place scripts variables(Per-request variables)

  • Variables defined in a pre-request script are available only within a single request that follows the script.
  • 변수 적용 범위: 하나의 요청
< {%  
	request.variables.set("key", "value")  
%}  

External file

  • .http
GET {{client.host.url}}
  • http-client.env.json
  • profile 별로 설정
{
  "dev":{
    "client": {
      "host": {
        "url": "example.org"
      }
    }
  }
}

Secret external file

  • passwords, tokens, certificates, and other sensitive information.
  • intellij를 사용한다면 .gitignore 파일에 추가하지 않아도 추적되지 않음
    If you use intellij, the http-client.private.env.json file is not tracked by Git.
  • 서드파티 툴을 사용한다면 .gitignore 파일에 수동으로 추가해줘야함
    If you do not use intellij(:use terminal or third-party tools), you may need to manually add http-client.private.env.json to .gitignore to avoid sharing confidential information
  • http-client.private.env.json

Dynamic variables

  • $uuid or $random.uuid: generates a universally unique identifier (UUID-v4)
  • $timestamp: generates the current UNIX timestamp
  • $isoTimestamp: generates the current timestamp in ISO-8601 format for the UTC timezone.
  • $randomInt: generates a random integer between 0 and 1000.
  • $random.integer(from, to): generates a random integer between from (inclusive) and to (exclusive), for example random.integer(100, 500). If you provide no parameters, it generates a random integer between 0 and 1000.
  • $random.float(from, to): generates a random floating point number between from (inclusive) and to (exclusive), for example random.float(10.5, 20.3). If you provide no parameters, it generates a random float between 0 and 1000.
  • $random.alphanumeric(length): generates a sequence of uppercase and lowercase letters, digits, and underscores of length length (must be greater than 0).
  • $random.hexadecimal(length): generates a random hexadecimal string of length length (must be greater than 0).
  • $random.email: generates a random email address.
  • $exampleServer: is replaced with the IntelliJ IDEA built-in web server, which can be accessed using HTTP Client only. The variable is used in GraphQL and WebSocket examples.

Iterate over collections in variables

< {%
    request.variables.set("id", [1,2,3,4,5])
%}

GET http://localhost:8080/books/{{id}}

http header

Cookies

  • 요청에 대한 응답으로 생성된 쿠키는 .idea/httpRequests/ 경로에 자동저장됩니다.
    The cookies received through a response are automatically saved into the dedicated http-client.cookies file under the .idea/httpRequests/ directory.
  • 저장: domain path name value date
    .example.com / userId 0x4d2 -1
응답 쿠키 미사용
// @no-cookie-jar
GET ~  
custom cookies
POST ~  
Cookie: key=first-value

Disable following redirects

// @no-redirect  
GET ~
profile
개발자가 하고싶어요

2개의 댓글

comment-user-thumbnail
2024년 10월 27일

오 또 올리셨군요 ㅋㅋ 잘 읽겠습니다.

1개의 답글