본문 바로가기
👩‍💻TIL/REST API

[REST API] URL 규칙, RESTful한 URL이란?

by devuna 2020. 8. 25.
728x90

REST API URL 규칙, RESTful한 URL이란?RESTful API

REST API 설계시 가장 중요한 항목은 아래 두가지이다.

1️⃣ URI는 정보의 자원을 표현해야 한다는 점
2️⃣ 자원에 대한 행위는 HTTP Method(GET, POST, PUT, DELETE)로 표현한다는 점

 

두 가지를 기억하며, 좀 더 RESTful한 URL을 설계해보자!(REST API에 대해 더 알아보려면? 👉 클릭  )

 

 

1. 소문자를 사용한다. 

주소에서 대소문자를 구분하므로, 카멜방식이 아닌 소문자를 사용하여 작성한다.

 

Bad

http://restapi.example.com/users/postComments

Good

http://restapi.example.com/users/post-comments

 

2. 언더바를 대신 하이픈을 사용한다.

가급적 하이픈의 사용도 최소화하며, 정확한 의미나 표현을 위해 단어의 결합이 불가피한 경우에 사용한다.

 

Bad

http://restapi.example.com/users/post_comments

Good

http://restapi.example.com/users/post-comments

 

3. 마지막에 슬래시를 포함하지 않는다.

슬래시는 계층을 구분하는 것으로, 마지막에는 사용하지 않는다.

 

Bad

http://restapi.example.com/users/

Good

http://restapi.example.com/users

 

4. 행위는 포함하지 않는다.

행위는 URL대신 Method를 사용하여 전달한다.(GET, POST, PUT, DELETE 등)

 

Bad

POST http://restapi.example.com/users/1/delete-post/1

Good

DELETE http://restapi.example.com/users/1/posts/1

 

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

REST API에서는 메시지 바디 내용의 포맷을 나타내기 위한 파일 확장자를 URI 안에 포함시키지 않습니다. Accept header를 사용하도록 한다.

 

Bad

http://restapi.example.com/users/photo.jpg

Good

GET http://restapi.example.com/users/photo

HTTP/1.1 Host: restapi.example.com Accept: image/jpg

 

6. 가급적 전달하고자하는 자원의 명사를 사용하되, 컨트롤 자원을 의미하는 경우 예외적으로 동사를 허용한다.

 

Bad

http://restapi.example.com/posts/duplicating

Good

http://restapi.example.com/posts/duplicate

 

 

728x90

댓글