반응형
DemoController
@RestController
public class DemoController {
@GetMapping("/api/demo")
public Map<String, Object> getDemo(
@RequestHeader(required = false) Map<String, Object> headers,
@RequestParam(required = false) Map<String, Object> params
) {
return Map.of(
"headers", headers,
"params", params
);
}
@PostMapping("/api/demo")
public Map<String, Object> postDemo(
@RequestHeader(required = false) Map<String, Object> headers,
@RequestParam(required = false) Map<String, Object> params,
@RequestBody(required = false) String body
) {
return Map.of(
"headers", headers,
"params", params,
"body", body
);
}
}
javacopy
GET 요청
./http/test.http
### GET 요청 GET http://localhost:8080/api/demo?key1=Hello&key2=World TX-ID: TX-0001 Cookie: key1=value1; key2=value2
plaintextcopy
response
http://localhost:8080/api/demo?key1=Hello&key2=World HTTP/1.1 200 Content-Type: application/json Transfer-Encoding: chunked Date: Sat, 27 Aug 2022 02:40:58 GMT Keep-Alive: timeout=60 Connection: keep-alive { "headers": { "tx-id": "TX-0001", "host": "localhost:8080", "connection": "Keep-Alive", "user-agent": "Apache-HttpClient/4.5.13 (Java/11.0.14.1)", "cookie": "key1=value1; key2=value2", "accept-encoding": "gzip,deflate" }, "params": { "key1": "Hello", "key2": "World" } } Response file saved. > 2022-08-27T114059.200.json Response code: 200; Time: 10ms; Content length: 248 bytes
plaintextcopy
POST 요청
./http/test.http
### POST 요청 POST http://localhost:8080/api/demo?key1=Hello&key2=World Content-Type: application/json TX-ID: TX-0001 Cookie: key1=value1; key2=value2 { "message": "Hello World" }
plaintextcopy
response
http://localhost:8080/api/demo?key1=Hello&key2=World HTTP/1.1 200 Content-Type: application/json Transfer-Encoding: chunked Date: Sat, 27 Aug 2022 02:43:12 GMT Keep-Alive: timeout=60 Connection: keep-alive { "headers": { "content-type": "application/json", "tx-id": "TX-0001", "content-length": "30", "host": "localhost:8080", "connection": "Keep-Alive", "user-agent": "Apache-HttpClient/4.5.13 (Java/11.0.14.1)", "cookie": "key1=value1; key2=value2", "accept-encoding": "gzip,deflate" }, "params": { "key1": "Hello", "key2": "World" }, "body": "{\n \"message\": \"Hello World\"\n}" } Response file saved. > 2022-08-27T114312.200.json Response code: 200; Time: 32ms; Content length: 350 bytes
plaintextcopy
POST 요청 - json 파일 활용
./http/post.json
{
"message": "Hello World"
}
jsoncopy
./http/test.http
### 파일 내용으로 POST 요청 POST http://localhost:8080/api/demo?key1=Hello&key2=World Content-Type: application/json TX-ID: TX-0001 Cookie: key1=value1; key2=value2 < ./post.json
plaintextcopy
response
http://localhost:8080/api/demo?key1=Hello&key2=World HTTP/1.1 200 Content-Type: application/json Transfer-Encoding: chunked Date: Sat, 27 Aug 2022 02:46:49 GMT Keep-Alive: timeout=60 Connection: keep-alive { "headers": { "content-type": "application/json", "tx-id": "TX-0001", "content-length": "32", "host": "localhost:8080", "connection": "Keep-Alive", "user-agent": "Apache-HttpClient/4.5.13 (Java/11.0.14.1)", "cookie": "key1=value1; key2=value2", "accept-encoding": "gzip,deflate" }, "params": { "key1": "Hello", "key2": "World" }, "body": "{\r\n \"message\": \"Hello World\"\r\n}" } Response file saved. > 2022-08-27T114650.200.json Response code: 200; Time: 18ms; Content length: 354 bytes
plaintextcopy
POST 요청 - multipart
### DEMO
POST http://localhost:8080/api/v1/file/upload
Authorization: Basic dGVzdDp0ZXN0
Content-Type: multipart/form-data; boundary=boundary
--boundary
Content-Disposition: form-data; name="files"; filename="HELP.md"
< ./HELP.md
--boundary
Content-Disposition: form-data; name="files"; filename="README.md"
< ./README.md
--boundary
Content-Disposition: form-data; name="message"
Hello World
--boundary
bashcopy
환경변수 사용하여 요청
./http/http-client.env.json
- 프로젝트 내에 http-client.env.json or rest-client.env.json 파일에 아래 내용 추가
{
"local": {
"host": "http://localhost",
"port": "8080"
},
"dev": {
"host": "https://dev.example.com",
"port": "9090"
}
}
jsoncopy
./http/test.http
- 실행시 환경(local or dev)을 선택 후 요청
### 환경변수 사용하여 요청 GET {{host}}:{{port}}/api/demo?key1=Hello&key2=World TX-ID: TX-0001 Cookie: key1=value1; key2=value2
plaintextcopy
response
http://localhost:8080/api/demo?key1=Hello&key2=World HTTP/1.1 200 Content-Type: application/json Transfer-Encoding: chunked Date: Sat, 27 Aug 2022 03:18:10 GMT Keep-Alive: timeout=60 Connection: keep-alive { "headers": { "tx-id": "TX-0001", "host": "localhost:8080", "connection": "Keep-Alive", "user-agent": "Apache-HttpClient/4.5.13 (Java/11.0.14.1)", "cookie": "key1=value1; key2=value2", "accept-encoding": "gzip,deflate" }, "params": { "key1": "Hello", "key2": "World" } } Response file saved. > 2022-08-27T121810.200.json Response code: 200; Time: 21ms; Content length: 248 bytes
plaintextcopy
Dynamic Variable 사용하여 요청
./http/test.http
- (1) 요청 후 Response Handler 탭에 찍히는 로그를 확인해서 Dynamic Variable이 잘 세팅되었는지 확인
- (2) 요청 후 위에서 세팅된 Dynamic Variable 값을 파라미터로 잘 전달하는지 확인
### (1) Dynamic Variable 사용하여 요청 POST http://localhost:8080/api/demo?key1=Hello&key2=World Content-Type: application/json { "message": "Hello World" } > {% var responseBody = JSON.parse(response.body.body); client.global.set("message", responseBody.message); client.log(client.global.get("message")); // 하단 Services > Response Handler 탭에 찍히는 로그 %} ### (2) 위에서 세팅한 값 활용하기 GET http://localhost:8080/api/demo?message={{message}}
plaintextcopy
response
http://localhost:8080/api/demo?message=Hello World HTTP/1.1 200 Content-Type: application/json Transfer-Encoding: chunked Date: Sat, 27 Aug 2022 03:33:41 GMT Keep-Alive: timeout=60 Connection: keep-alive { "headers": { "host": "localhost:8080", "connection": "Keep-Alive", "user-agent": "Apache-HttpClient/4.5.13 (Java/11.0.14.1)", "accept-encoding": "gzip,deflate" }, "params": { "message": "Hello World" } } Response file saved. > 2022-08-27T123341.200.json Response code: 200; Time: 20ms; Content length: 188 bytes
plaintextcopy
Dynamic Variable 사용하기 - js 스크립트 활용
./http/dynamic_variable.js
var responseBody = JSON.parse(response.body.body); client.global.set("message", responseBody.message); client.log(client.global.get("message")); // 하단 Services > Response Handler 탭에 찍히는 로그
plaintextcopy
./http/test.http
### Dynamic Variable 사용하여 요청 POST http://localhost:8080/api/demo?key1=Hello&key2=World Content-Type: application/json { "message": "Hello World" } > ./dynamic_variable.js ### 위에서 세팅한 값 활용하기 GET http://localhost:8080/api/demo?message={{message}}
plaintextcopy
response
http://localhost:8080/api/demo?message=Hello World HTTP/1.1 200 Content-Type: application/json Transfer-Encoding: chunked Date: Sat, 27 Aug 2022 03:45:47 GMT Keep-Alive: timeout=60 Connection: keep-alive { "headers": { "host": "localhost:8080", "connection": "Keep-Alive", "user-agent": "Apache-HttpClient/4.5.13 (Java/11.0.14.1)", "accept-encoding": "gzip,deflate" }, "params": { "message": "Hello World" } } Response file saved. > 2022-08-27T124548.200.json Response code: 200; Time: 13ms; Content length: 188 bytes
plaintextcopy
요청 결과를 테스트
./http/test.http
### 요청 결과를 테스트 POST http://localhost:8080/api/demo?key1=Hello&key2=World Content-Type: application/json { "message": "Hello World" } > {% client.test("Request executed successfully", function() { // 실패시 하단의 Services > Tests 탭에 실패 항목 출력 client.assert(response.status === 200, "Response status is not 200"); client.assert(JSON.parse(response.body.body).message === "Hello World", "응답 메시지가 'Hello World'가 아니다") }); %}
plaintextcopy
참고
- https://jojoldu.tistory.com/266
- https://blog2.deliwind.com/20200405/IntelliJ%EC%97%90%EC%84%9C-http%EB%A1%9C-%ED%85%8C%EC%8A%A4%ED%8A%B8%ED%95%98%EA%B8%B0/
반응형
'Development > IntelliJ' 카테고리의 다른 글
[IntelliJ] 플러그인 만들기 (0) | 2023.07.30 |
---|---|
[IntelliJ] 정규식으로 문자열 치환 (0) | 2019.03.20 |