Request
: HTTP Request(μμ²)λ ν΄λΌμ΄μΈνΈκ° μλ²λ‘ 보λ΄λ μμ² λ©μμ§λ‘, μΉ λ¦¬μμ€μ λν λμ(GET, POST, λ±) λ° ν΄λΉ 리μμ€μ ν¨κ» μ λ¬λλ λ°μ΄ν°λ₯Ό μ μν©λλ€.
Overview
β’
ν΄λΌμ΄μΈνΈκ° μλ²λ‘ μμ²μ 보λ΄λ λ°©λ²
β¦
λΈλΌμ°μ λ₯Ό ν΅ν΄ μμ² λ³΄λ΄κΈ°
β¦
API κ°λ° λ° ν
μ€νΈ λꡬλ₯Ό ν΅ν΄ μμ² λ³΄λ΄κΈ°
βͺ
Postman
βͺ
Thunder Client
β’
Thunder Client λ‘ μμ² λ³΄λ΄λ³΄κΈ°
β’
μ€νλ§ λΆνΈλ‘ μλ²μμ μμ² λ°μ보기
β¦
μμ² ν
μ€νΈ
βͺ
Thunder Client λ‘ μμ² ν
μ€νΈ
βͺ
HTML form νκ·Έλ‘ μμ² ν
μ€νΈ
ν΄λΌμ΄μΈνΈκ° μλ²λ‘ μμ²μ 보λ΄λ λ°©λ²
β’
λΈλΌμ°μ λ₯Ό ν΅ν΄ μμ² λ³΄λ΄κΈ°
β’
API κ°λ° λ° ν
μ€νΈ λꡬλ₯Ό ν΅ν΄ μμ² λ³΄λ΄κΈ°
λΈλΌμ°μ λ₯Ό ν΅ν΄ μμ² λ³΄λ΄κΈ°
β’
μ£Όμ νμμ€μ URL λ₯Ό μ
λ ₯νμ¬ μμ²νκΈ°
β’
FORM μ μ‘ μμ² νκΈ°
μ£Όμ νμμ€μ URLμ μ λ ₯νμ¬ μμ²νκΈ°:
β’
HTTP μμ² λ©μλ: μΌλ°μ μΌλ‘ λΈλΌμ°μ μμ μ£Όμ νμμ€μ URLμ μ
λ ₯νμ¬ μμ²μ λ³΄λΌ λ μ¬μ©λλ HTTP λ©μλλ GETμ
λλ€.
β’
νΉμ§:
β¦
GET λ©μλλ μλ²λ‘λΆν° μ 보λ₯Ό μμ²νκ³ ν΄λΉ 리μμ€μ λν μμ²μ 보λ
λλ€.
β¦
μ£Όμ νμμ€μ μ§μ μ
λ ₯νλ―λ‘, λΈλΌμ°μ μ μν΄ κ°μ₯ κ°λ¨νκ² μμ²ν μ μμ΅λλ€.
β¦
μ£Όλ‘ μΉνμ΄μ§λ₯Ό μμ²ν λ μ¬μ©λλ©°, URLμ 쿼리 νλΌλ―Έν°λ₯Ό ν¬ν¨νμ¬ μ 보λ₯Ό μ λ¬ν μ μμ΅λλ€.
FORM μ μ‘ μμ²νκΈ°:
β’
HTTP μμ² λ©μλ: FORM μ μ‘μμ μ¬μ©λλ κΈ°λ³Έ λ©μλλ GET λλ POSTμ
λλ€.
β’
νΉμ§:
β¦
GET: νΌ λ°μ΄ν°λ₯Ό URLμ 쿼리μ€νΈλ§μ ν¬ν¨νμ¬ μ μ‘ν©λλ€. μ£Όλ‘ λ°μ΄ν° κ²μμ΄λ μ‘°νμ μ¬μ©λ©λλ€. URLμ λ°μ΄ν°κ° λ
ΈμΆλλ©° κΈΈμ΄μ μ μ½μ΄ μμ΅λλ€.
β¦
POST: νΌ λ°μ΄ν°λ₯Ό HTTP μμ²μ λ³Έλ¬Έ(body)μ ν¬ν¨νμ¬ μ μ‘ν©λλ€. λ°μ΄ν° κΈΈμ΄μ μ μ½μ΄ μκ³ , λ―Όκ°ν μ 보 μ μ‘μ μ¬μ©λ©λλ€. μμ νκ³ λ°μ΄ν°λ₯Ό λ
ΈμΆμν€μ§ μμ΅λλ€.
<form action="/board/insert" method="POST" id="form">
<input type="text" name="title" placeholder="μ λͺ©">
<input type="text" name="writer" placeholder="μμ±μ">
<textarea name="content" cols="40" rows="5" placeholder="λ΄μ©"></textarea>
</form>
HTML
볡μ¬
GET vs POST
GET
μ©λ
β’
μλ²λ‘ λ°μ΄ν°λ₯Ό μμ²νμ¬ κ°μ Έμ€λ λ° μ¬μ©λ¨.
β’
μ 보λ₯Ό κ²μνκ±°λ μ‘°νν λ μ¬μ©.
νΉμ§
β’
URLμ 쿼리 νλΌλ―Έν°λ₯Ό ν΅ν΄ λ°μ΄ν°λ₯Ό μ λ¬
β’
κΈΈμ΄μ μ νμ΄ μμ΄ νμ λ μμ λ°μ΄ν° μ λ¬μ μ ν©.
β’
λΈλΌμ°μ μμ μΊμ±λ μ μμ΄, λ°λ³΅ μμ²μ μ μ©ν¨.
POST
μ©λ
β’
μλ²λ‘ λ°μ΄ν°λ₯Ό μ μ‘νλ λ° μ¬μ©λ¨.
β’
μλ‘μ΄ λ°μ΄ν°λ₯Ό μμ±νκ±°λ μ
λ°μ΄νΈν λ μ¬μ©.
νΉμ§
β’
λ°μ΄ν°λ₯Ό HTTP μμ²μ λ³Έλ¬Έ(body)μ ν¬ν¨νμ¬ μ λ¬ (λ°μ΄ν°κ° URLμ λ
ΈμΆλμ§ μμ).
β’
λ°μ΄ν° κΈΈμ΄μ μ νμ΄ μμ΄ λ λ§μ μμ λ°μ΄ν° μ λ¬μ μ ν©.
β’
λ―Όκ°ν μ 보 μ μ‘μ μ ν©νλ©°, 보μμ μν HTTPS μ¬μ© κΆμ₯.
API κ°λ° ν μ€νΈ λꡬλ₯Ό ν΅ν΄ μμ² λ³΄λ΄κΈ°
API κ°λ° ν μ€νΈ λꡬ
: κ°λ°μκ° APIλ₯Ό λ§λ€κ³ , ν
μ€νΈνλ©°, λλ²κΉ
νλλ° λμμ μ£Όλ μννΈμ¨μ΄ λꡬμ
λλ€.
μ’ λ₯
β’
Postman
β’
Thunder Client (VSCODE)
Postman
β’
API κ°λ°, ν
μ€νΈ λ° λ¬Έμνμ λ리 μ¬μ©λλ νλ«νΌ.
β’
μ¬μ©μ μΉνμ μΈν°νμ΄μ€μ λ€μν κΈ°λ₯μ μ 곡νμ¬ API ν
μ€νΈ λ° λλ²κΉ
μ΄ μ©μ΄ν¨.
Thunder Client (VSCODE)
κ²½λ Rest API ν΄λΌμ΄μΈνΈ μ΅μ€ν
μ
μΌλ‘, Visual Studio Codeμ©μΌλ‘ μ€κ³λ κ°λ¨νκ³ κΉλν λμμΈμ κ°μ§κ³ μμ΅λλ€.
Postman μΌλ‘ μμ² λ³΄λ΄λ³΄κΈ°
1.
New
2.
HTTP Request
3.
4.
Send
New
HTTP Request
Request μμ±
1.
μμ² λ©μλ : GET
2.
URL : https://httpbin.org/get
Send
μλ΅ λ©μμ§
{
"args": {},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate, br",
"Host": "httpbin.org",
"Postman-Token": "86f7879d-21ba-425e-a061-4d640b5dd900",
"User-Agent": "PostmanRuntime/7.31.1",
"X-Amzn-Trace-Id": "Root=1-6540f7d5-3e6af9f251d571d0093f073f"
},
"origin": "114.201.106.211",
"url": "https://httpbin.org/get"
}
JSON
볡μ¬
Thunder Client λ‘ μμ² λ³΄λ΄λ³΄κΈ°
1.
VS CODE μ€ν
2.
νμ₯: λ§μΌνλ μ΄μ€ (Extension)
3.
Thnder Client κ²μ
4.
μ€μΉ
5.
Thnder Client μμ΄μ½ ν΄λ¦
6.
New Request
7.
8.
Send (μμ²-Requset)
9.
κ²°κ³Ό νμΈ (μλ΅-Response)
λ μμΈν μ¬μ©λ²
μ€μΉ
β’
VS CODE μ€ν
β’
νμ₯: λ§μΌνλ μ΄μ€ (Extension)
β’
Thunder Client κ²μ
Thunder Client μμ΄μ½ ν΄λ¦
New Request
Request μμ±
1.
μμ² λ©μλ : GET
Send
μλ΅ νμΈ
μ€νλ§ λΆνΈλ‘ μλ²μμ μμ² λ°μ보기
β’
νλ‘μ νΈ κ΅¬μ‘°
β’
νλ‘μ νΈ νκ²½
β’
μμ
νμΌ
β’
μ€ν κ²°κ³Ό
β¦
μμ² ν
μ€νΈ
βͺ
GET
βͺ
POST
βͺ
PUT
βͺ
DELETE
νλ‘μ νΈ κ΅¬μ‘°
β β β βββ
dto/
β β β β βββ
Board.java
β β β βββ
ServletInitializer.java
β β β βββ
SpringRequestApplication.java
β βββ
resources/
β β βββ
application.properties
β β βββ
static/
β β βββ
templates/
βββ
build.gradle
νλ‘μ νΈ νκ²½
β’
Spring boot version : 3.3.5
β’
Group Id : com.aloha
β’
Artifact Id : spring-request
β’
Java version : JDK 17
β’
Packaging : WAR
β’
Dependency
β¦
Spring Web
β¦
Spring Dev Tools
β¦
Lombok
μμ νμΌ
β’
build.gradle
β’
Board.java
β’
BoardController.java
β’
build.gradle
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
compileOnly 'org.projectlombok:lombok'
developmentOnly 'org.springframework.boot:spring-boot-devtools'
annotationProcessor 'org.projectlombok:lombok'
providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
SQL
볡μ¬
β’
Board.java
/**
* Board
* - κ²μκΈ μ 보
*/
@Data
@NoArgsConstructor
public class Board {
private int boardNo;
private String title;
private String writer;
private String content;
private Date regDate;
private Date updDate;
private int views;
public Board(String title, String writer, String content) {
this.title = title;
this.writer = writer;
this.content = content;
}
}
Java
볡μ¬
β’
BoardController.java
/**
* κ²μν 컨νΈλ‘€λ¬
* - κ²μκΈ λͺ©λ‘ - [GET] - /board
* - κ²μκΈ μ‘°ν - [GET] - /board/10
* - κ²μκΈ λ±λ‘ μ²λ¦¬ - [POST] - /board
* - κ²μκΈ μμ μ²λ¦¬ - [PUT] - /board
* - κ²μκΈ μμ μ²λ¦¬ - [DELETE] - /board
*/
@Slf4j // λ‘κ·Έ μ¬μ© μ΄λ
Έν
μ΄μ
// @Controller // ν΄λΉ ν΄λμ€λ₯Ό 컨νΈλ‘€λ¬ μν λ‘ λΉλ±λ‘
@RestController // @ResponseBody+@Controller : ν΄λΉ ν΄λμ€λ₯Ό REST API 컨νΈλ‘€λ¬λ‘ λΉλ±λ‘
@RequestMapping("/board")
public class BoardController {
/**
* κ²μκΈ λͺ©λ‘
* [GET]
* /board
* - response : boardList
* @return
* @throws Exception
*/
@GetMapping(value="")
public ResponseEntity<List<Board>> list() throws Exception {
log.info("[GET] - /board");
List<Board> boardList = new ArrayList<>();
boardList.add(new Board("μ λͺ©1", "μμ±μ1", "λ΄μ©1"));
boardList.add(new Board("μ λͺ©2", "μμ±μ2", "λ΄μ©2"));
boardList.add(new Board("μ λͺ©3", "μμ±μ3", "λ΄μ©3"));
return new ResponseEntity<>(boardList, HttpStatus.OK);
}
/**
* κ²μκΈ μ‘°ν
* [GET]
* /board
* - response : board
* @param model
* @param boardNo
* @return
* @throws Exception
*/
@GetMapping(value="/{boardNo}")
public ResponseEntity<Board> read(@PathVariable("boardNo") int boardNo) throws Exception {
log.info("[GET] - /board/read");
Board board = new Board("μ λͺ©1", "μμ±μ1", "λ΄μ©1");
return new ResponseEntity<>(board, HttpStatus.OK);
}
/**
* κ²μκΈ μ°κΈ° μ²λ¦¬
* [POST]
* /board
* - response :
* β
μμ² μ±κ³΅ : 200 OK "SUCCESS"
* β μμ² μ€ν¨ : 400 Bad Request "FAIL"
* @param board
* @return
* @throws Exception
*/
@PostMapping(value="")
public ResponseEntity<String> insertPro(Board board) throws Exception {
int result = new Random().nextInt(2);
if( result == 0 ) {
return new ResponseEntity<>("FAIL", HttpStatus.BAD_REQUEST);
}
return new ResponseEntity<>("SUCCESS", HttpStatus.OK);
}
/**
* κ²μκΈ μμ μ²λ¦¬
* [PUT]
* /board
* - response :
* β
μμ² μ±κ³΅ : 200 OK "SUCCESS"
* β μμ² μ€ν¨ : 400 Bad Request "FAIL"
* @param board
* @return
* @throws Exception
*/
@PutMapping(value="")
public ResponseEntity<String> updatePro(Board board) throws Exception {
int result = new Random().nextInt(2);
if( result == 0 ) {
return new ResponseEntity<>("FAIL", HttpStatus.BAD_REQUEST);
}
return new ResponseEntity<>("SUCCESS", HttpStatus.OK);
}
/**
* κ²μκΈ μμ μ²λ¦¬
* [DELETE]
* /board
* - response :
* β
μμ² μ±κ³΅ : 200 OK "SUCCESS"
* β μμ² μ€ν¨ : 400 Bad Request "FAIL"
* @param boardNo
* @return
* @throws Exception
*/
@DeleteMapping(value="")
public ResponseEntity<String> deletePro(@RequestParam("boardNo") int boardNo) throws Exception {
log.info("boardNo : " + boardNo);
int result = new Random().nextInt(2);
if( result == 0 ) {
return new ResponseEntity<>("FAIL", HttpStatus.BAD_REQUEST);
}
return new ResponseEntity<>("SUCCESS", HttpStatus.OK);
}
}
Java
볡μ¬
μ€ν κ²°κ³Ό
μμ² ν μ€νΈ
β’
Thunder Client λ‘ μμ² ν
μ€νΈ
β’
HTML form νκ·Έλ‘ μμ² ν
μ€νΈ
β’
β’
β’
POST : http://localhost:8080/board
β¦
Body
Form
title, writer, content
β’
β¦
Body
Form
boardNo, title, writer, content
β’
DELETE : http://localhost:8080/board/10
Thunder Client λ‘ μμ² ν μ€νΈ
POST : http://localhost:8080/board
β’
Body
Form
title, writer, content
β’
Body
Form
boardNo, title, writer, content
DELETE : http://localhost:8080/board/10
Run Collection
HTML form νκ·Έλ‘ μμ² ν μ€νΈ
β’
build.gradle
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
Plain Text
볡μ¬
β’
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>HTML form νκ·Έλ‘ μμ² ν
μ€νΈ</title>
</head>
<body>
<h1>HTML form νκ·Έλ‘ μμ² ν
μ€νΈ</h1>
<p>
POST, PUT, DELETE λ μ€μ DB μ μ°κ²°μ΄ μ λμ΄ μκ³ , <br>
λλ€μΌλ‘ SUCCESS, FAIL μλ΅ν©λλ€.
</p>
<hr>
<ul>
<li>GET : <a href="http://localhost:8080/board">http://localhost:8080/board</a></li>
<li>GET : <a href="http://localhost:8080/board/10">http://localhost:8080/board/10</a></li>
<li>
POST : <a href="http://localhost:8080/board">http://localhost:8080/board</a>
<ul>
<li>Body β‘ Form β‘ title, writer, content</li>
</ul>
</li>
<li>
PUT : <a href="http://localhost:8080/board">http://localhost:8080/board</a>
<ul>
<li>Body β‘ Form β‘ boardNo, title, writer, content</li>
</ul>
</li>
<li>
DELETE : <a href="http://localhost:8080/board">http://localhost:8080/board/10</a>
<ul>
<li>Body β‘ Form β‘ boardNo</li>
</ul>
</li>
</ul>
<hr>
<h2>- GET : http://localhost:8080/board</h2>
<form action="/board" method="GET">
<input type="submit" value="μμ²">
</form>
<hr>
<h2>- GET : http://localhost:8080/board/10</h2>
<form action="/board/10" method="GET">
<input type="submit" value="μμ²">
</form>
<hr>
<h2>- POST : http://localhost:8080/board</h2>
<h3>- Body β‘ Form β‘ title, writer, content</h3>
<form action="/board" method="POST">
<input type="text" name="title" placeholder="title" value="μ λͺ©"> <br>
<input type="text" name="writer" placeholder="writer" value="μμ±μ"> <br>
<input type="text" name="content" placeholder="content" value="λ΄μ©"> <br>
<input type="submit" value="μμ²">
</form>
<hr>
<h2>- PUT : http://localhost:8080/board</h2>
<h3>- Body β‘ Form β‘ boardNo, title, writer, content</h3>
<!-- form νκ·Έμμλ method: GET,POST λ§ μ§μλ¨ -->
<form action="/board" method="POST" id="form-put">
<input type="text" name="boardNo" placeholder="boardNo" value="10"> <br>
<input type="text" name="title" placeholder="title" value="μ λͺ©"> <br>
<input type="text" name="writer" placeholder="writer" value="μμ±μ"> <br>
<input type="text" name="content" placeholder="content" value="λ΄μ©"> <br>
<input type="button" value="μμ²" onclick="update()">
</form>
<hr>
<h2>- DELETE : http://localhost:8080/board</h2>
<h3>- Body β‘ Form β‘ boardNo</h3>
<!-- form νκ·Έμμλ method: GET,POST λ§ μ§μλ¨ -->
<form action="/board" method="POST" id="form-delete">
<input type="text" name="boardNo" placeholder="boardNo" value="10"> <br>
<input type="button" value="μμ²" onclick="remove()">
</form>
<hr>
<script>
// κ²μκΈ μμ μμ² - JSON
function update() {
let request = new XMLHttpRequest()
// μ
λ ₯ μ 보
let form = document.getElementById('form-put')
let boardNo = form.boardNo.value
let title = form.title.value
let writer = form.writer.value
let content = form.content.value
// JSON
let data = {
'boardNo' : boardNo,
'title' : title,
'writer' : writer,
'content' : content
}
// μλ΅ νμΈ
request.onreadystatechange = function() {
// μμ² μ±κ³΅
if( request.readyState == request.DONE && request.status == 200 ) {
let response = request.responseText
alert(response)
}
// μμ² μ€ν¨
if( request.readyState == request.DONE && request.status == 400 ) {
let response = request.responseText
alert(response)
}
// μλ¬ λ°μ
if( request.readyState == request.DONE && request.status == 500 ) {
alert('μλ²μΈ‘ μλ¬ λ°μ')
}
}
// μμ² μ€μ
let url = 'http://localhost:8080/board'
request.open("PUT", url, true)
request.setRequestHeader("Content-Type", "application/json")
// JSON.stringify() : JavaScript μ κ°μ²΄λ₯Ό JSON λ¬Έμμ΄λ‘ λ³ννλ λ©μλ
request.send( JSON.stringify(data) )
}
// κ²μκΈ μμ μμ² - JSON
function remove() {
let request = new XMLHttpRequest()
// μ
λ ₯ μ 보
let form = document.getElementById('form-put')
let boardNo = form.boardNo.value
// JSON
let data = {
'boardNo' : boardNo,
}
// μλ΅ νμΈ
request.onreadystatechange = function() {
// μμ² μ±κ³΅
if( request.readyState == request.DONE && request.status == 200 ) {
let response = request.responseText
alert(response)
}
// μμ² μ€ν¨
if( request.readyState == request.DONE && request.status == 400 ) {
let response = request.responseText
alert(response)
}
// μλ¬ λ°μ
if( request.readyState == request.DONE && request.status == 500 ) {
alert('μλ²μΈ‘ μλ¬ λ°μ')
}
}
// μμ² μ€μ
let url = 'http://localhost:8080/board/' + boardNo
request.open("DELETE", url, true)
// JSON.stringify() : JavaScript μ κ°μ²΄λ₯Ό JSON λ¬Έμμ΄λ‘ λ³ννλ λ©μλ
request.send()
}
</script>
</body>
</html>
HTML
볡μ¬






























