먼저!!
서버가 보내준 것은 리소스가 아니다
아래와 같이 HTTP GET 요청을 서버로 보내서
GET https://example.org/greeting
Host: example.org
Accept: text/plain, text/html; q=0.9 *; q=0.1
Accept-Language: en, ko; q=0.9, *; q=0.1
"hello" 라는 메시지 응답을 받았다고 해보자
HTTP/1.1 200 OK
Content-Length: 6
Date: Sun, 19 Mar 2017 10:20:47 GMT
Last-Modified: Sun, 19 Mar 2017 08:00:00 GMT
Content-Type: text/plain
Content-Language: en
hello
이러한 상황을 https://example.org/greeting 라는 URI 요청해서 "hello" 라는 리소스를 응답으로 받았다고 표현하는 경우가 있다.
하지만, 정확히 말하면 틀린 표현이다.
왜냐하면 "hello"는 리소스가 아니라 representation data 이기 때문이다.
"현재의 선택된 representation" 이란 무엇인가?
먼저, "representation" 은 무엇인가? 어떤 리소스의 특정 시점의 상태를 반영하고 있는 정보이다.
representation = representation data + representation metadata
representation data : "hello"
representation metadata : "Content-Type:text/plain" 과 "Content-Language:en"
"현재"란 무엇인가?
https://example.org/greeting 가 가리키는 리소스의 representation이 "hi"에서 "hello"로 수정되었다면 "현재" representation은 "hi" 가 아닌 "hello" 가 될 것이다.
"선택된"이라고 함은 무슨 뜻일까?
하나의 리소스의 현재 representation 이 하나 이상이 될 수 있으며, 그 중 하나가 선택되엇음을 의미한다. 즉, "greeting" 리소스의 현재 representation은, 영어 사용자를 위한 "hello", 한국어 사용자를 위한 "안녕하세요", HTML 문서를 원하는 클라이언트를 위한 "hello" 등 여러가지가 될 수 있는데, 이들 중 하나가 선택되었다는 의미이다.
metadata를 포함하여 representation들의 예를 들어보자
영어 사용자를 위한 representation:
Content-Type: text/plain
Content-Language: en
hello
한국어 사용자를 위한 representation:
Content-Type: text/plain
Content-Language: ko
안녕하세요
HTML을 선호하는 영어 사용자를 위한 representation:
Content-Type: text/html; charset=UTF-8
Content-Language: en
<html><body>hello</body></html>
HTML을 선호하는 한국어 사용자를 위한 representation:
Content-Type: text/html; charset=UTF-8
Content-Language: ko
<html><body>안녕하세요</body></html>
"선택" 하는 것은 누가 어떻게 하는가?
클라이언트와 서버간의 내용 협상(Content negotiation)을 통해 선택하며, 선택의 주체는 협상 방법에 따라 다르다. 사전 협상(proactive negotiation)의 경우 서버가 선택한다.
클라이언트의 GET 요청에 "Accept-Language:ko" 헤더를 포함시켜 한국어를 선호함을 밝혔다면, 서버는 가장 적절한 representation인 "안녕하세요"로 응답을 할 것이며, 만약 "Accept:text/html; charset=UTF-8" 헤더도 포함시켜 한국어로 되어있는 HTML 문서를 선호한다고 했다면, "안녕하세요" representation 을 응답할 것이다.
representation이 존재하지 않는다면 406 Not Acceptable로 응답할 것이다.
이러한 선택을 "여러 리소스 중 하나가 선택되었다." 라고 말할 수 없는 이유는 무엇인가?
"안녕하세요" 나 "hello" 모두 uri가 동일하기 때문이다.
수정중~~
참고 블로그