REST API

heyj·2022년 3월 17일
0

네트워크

목록 보기
2/3

REST API?

REST API에서 'REST'는 Representational State Transfer의 약자로, 웹(HTTP)의 장점을 최대한 활용할 수 있는 아키텍처 입니다. REST의 구성요소는 자원(resource) - URI, 행위(verb) - HTTP Method, 표현으로 이루어져 있습니다.

URI, URL 차이?

  • URI는 특정 리소스를 식별하는 통합 자원 식별자(uniform resource identifier)를 의미한다. 웹 기술에서 사용하는 논리적 또는 물리적 리소스를 식별한다.
  • URL은 웹 주소이며, 컴퓨터 네트워크 상에서 리스소스 어디에 있는지 알려주기 위한 규약으로, URI의 서브셋이다.

REST API 규칙

1. REST APIs use Uniform Resource Identifiers (URIs) to address resources.

웹에서 사용되는 데이터나 자원을 HTTP URI로 표현해야 합니다.
URI는 자원을 표현하는 데 중점을 두어야 하므로 행위(동사) 표현이 들어가면 안됩니다.

2. Use HTTP Method(GET, POST, PUT, DELETE)

자원에 대한 행위는 HTTP METHOD로 표현합니다.

보통 CRUD에서 조회는 GET, 등록은 POST, 수정은 PUT, 삭제는 DELETE를 이용

  • POST vs PUT : 멱등성(여러번 수행해도 결과가 같음)으로 POST와 PUT을 구분할 수 있는데, POST는 매 호출마다 새로운 데이터가 추가되지만 PUT은 반복 수행해도 결과가 같다.

  • PUT vs. PATCH : PATCH는 데이터의 부분만 수정하고 싶을 때 사용하는 메소드로 수정이 필요한 부분만을 요청에 담지만, PUT은 데이터 전체를 바꾸기 때문에 수정할 부분 + 바뀌지 않아야 하는 속성까지 모두 요청에 담아야 합니다.

3. Lowercase letters should be preferred in URI paths.

소문자 URI를 사용합니다. RFC3986(URI 문법 형식)은 URI 스키마와 호스트를 제외하고 대소문자를 구별하도록 규정하고 있습니다.

1) http://api.example.com/my-folder/my-doc [o]
2) HTTP://API.EXAMPLE.COM/my-folder/my-doc [o]

3) http://api.example.com/My-Folder/my-doc [x]

4. Hyphens (-) should be used to improve the readability of URIs. Underscores (_) should not be used in URIs.

언더바 대신 하이픈을 사용합니다.

http://api.example.com/blogs/guy-levin/posts/this-is-my-first-post [o]
http://api.example.com/blogs/guy-levin/posts/this_is_my_first_post [x]

5. Forward slash separator (/) must be used to indicate a hierarchical relationship.

계층 관계를 나타낼 때는 슬래시로 구분합니다.

http://api.canvas.com/shapes/polygons/quadrilaterals/squares

6. A trailing forward slash (/) should not be included in URIs.

URI의 마지막에는 슬래시(/)를 포함하지 않습니다.

http://api.canvas.com/shapes/ [x]
http://api.canvas.com/shapes  [o]

7. File extensions should not be included in URIs.

파일 확장자는 URI에 포함시키지 않습니다.

http://api.college.com/students/3248234/courses/2005/fall.json [x]
http://api.college.com/students/3248234/courses/2005/fall [o]

8. the endpoint name should be plural

endpoint 이름은 복수형으로 작성합니다.

http://api.college.com/students/3248234/courses - Retrieves a list of all courses that are learned by a student with id 3248234.
http://api.college.com/students/3248234/courses/physics - Retrieves course physics for a student with id 3248234.

참고
https://velog.io/@pjh612/REST-API-URI-%EA%B7%9C%EC%B9%99
https://velog.io/@stampid/REST-API%EC%99%80-RESTful-API
https://restfulapi.net/rest-put-vs-post/
https://dzone.com/articles/7-rules-for-rest-api-uri-design-1

이후 추가 포스팅해야할 내용

0개의 댓글