Deview 2017 '그런 REST API로 괜찮은가'를 보고 정리한 글입니다
👉 이는 REST의 제약조건 중 'self-descriptive'와 'HATEOAS'를 만족하지 못하기 때문이다
self-descriptive?
HATEOAS?
- REST를 구성하는 스타일:
client-server, stateless, cache, uniform-interface, layered system, code-on-demand(optional)
- uniform-interface의 제약 조건 중에
self-descriptive messages
hypermedia as the engine of application state (HATEOAS)이 있다
HTTP/1.1 200 OK
Content-Type: application/json
[ { "op": "remove", "path": "/a/b/c" } ]
vs.
HTTP/1.1 200 OK
Content-Type: application/json-patch+json
[ { "op": "remove", "path": "/a/b/c" } ]
A: 아래가 self-descriptive하다. 왜냐하면 application/json-patch+json
명세에 "op"와 "path"가 정의되어 있기 때문이다. 단순히 위와 같이 메시지를 작성한 경우, 우리는 "op"와 "path"가 뭔지 알 수 없다. 즉, self-descriptive하지 않다!
1) HTML의 경우: HATEOAS를 잘 지켰다
HTTP/1.1 200 OK
Content-Type: text/html
<html>
<head></head>
<body><a href="/test">test</a></body>
</html>
2) HTTP API: 헤더에 링크를 넣음으로써 HATEOAS를 지켰다
HTTP/1.1 200 OK
Content-Type: application/json
Link: </articles/1>; rel="previous",
</articles/3>; rel="next;
{
"title": "The second article",
"contents": "blah blah..."
}
👉 결국, REST를 따를 것인지 말지는 API를 설계하는 이들이 판단해서 결정할 일이다
A: 긴 시간에 걸쳐 진화하는 웹 애플리케이션을 위한 것이다
REST emphasizes evolvability to sustain an uncontrollable system. If you think you have control over the system or aren’t interested in evolvability, don’t waste your time arguing about REST.
- Roy T. Fielding -
Roy: 아니, 네 맘대로 해
대신 너가 시스템의 완벽한 통제권을 가졌거나 그 진화에 관심없다면 말이야!
I am getting frustrated by the number of people calling any HTTP-based interface a REST API. ... Please try to adhere to them or choose some other buzzword for your API.
- Roy T. Fielding -