데이터 전송 방식은 크게 2가지로 나눌 수 있다.
GET
POST
, PUT
, PATCH
4가지 예시
정적 데이터 조회 : 이미지, 정적 텍스트 문서를 GET으로 조회
한다. 정적 데이터는 쿼리 파라미터 없이 리소스 경로만
으로 조회할 수 있다.
GET /static/image.jpg HTTP/1.1
Host: localhost:8080
동적 데이터 조회 : 서버는 클라이언트에서 전달받은 쿼리 스트링 기반
으로 정렬 필터해서 결과를 동적으로 생성한다. GET은 쿼리 파라미터를 사용해서 서버로 데이터를 전달하고 주로 검색과 게시판 목록에서 정렬 필터(검색어)에서 사용한다.
GET /search?q=hello&hl=ko HTTP/1.1
Host: www.google.com
HTML Form을 통한 데이터 전송
application/x-www-form-urlencoded
3-1. 예시
<form action="/save" method="post">
<input type="text" name="username" />
<input type="text" name="age" />
<button type="submit">전송</button>
</form>
HTML form에 입력된 정보를 갖고 웹 브라우저는 요청 HTTP 메시지를 생성한다.
POST /save HTTP/1.1
Host: localhost:8080
Content-Type: application/x-www-form-urlencoded
username=kim&age=20
3-2. 예시
사용자의 이름과 나이 그리고 파일까지 전송해야 되는 경우
<form action="/save" method="post" enctype="multipart/form-data">
<input type="text" name="username" />
<input type="text" name="age" />
<input type="file" name="file1" />
<button type="submit">전송</button>
</form>
웹 브라우저가 생성한 요청 HTTP 메시지
multipart로 나눠져서 데이터들이 전송된다.
POST /save HTTP/1.1
Host: localhost:8080
Content-Type: multipart/form-data; boundary=----XXX
Content-Length: 10457
------XXX
Content-Disposition: form-data; name="username"
kim
------XXX
Content-Disposition: form-data; name="age"
20
------XXX
Content-Disposition: form-data; name="file1";filename="intro.png"
Content-Type: image/png
109495782agfgjlkh561...
------XXX--
HTTP API를 통한 데이터 전송
회원 가입, 상품 주문, 데이터 변경할 때 사용한다.
application/json
을 주로 사용한다. (Text, xml, json)POST /members HTTP/1.1
Content-Type: application/json
{
"username":"kim",
"age":20
}