Spring boot
RestAPI 주소 설계 규칙
haejujung
2024. 11. 6. 14:25
- 소문자를 사용한다.
대문자는 때로 문제를 일으키는 경우가 있기 때문에 URI를 작성할 때는 소문자를 선호한다.
❌http://dev-cool.tistory.com/users/Post-Comments
⭕http://cocoon1787.tistory.com/users/post-comments
- 언더바(_) 대신 하이픈(-)을 사용한다.
가독성을 위해 긴 Path를 표현하는 단어는 하이픈(-)으로 구분하는 것이 좋다.
프로그램의 글자 폰트에 따라 언더바 문자는 부분적으로 가려지거나 숨겨질수 있다.
❌http://dev-cool.tistory.com/users/post_comments
⭕http://dev-cool.tistory.com/users/post-comments
- 마지막에 슬래시(/)를 포함하지 않는다.
후행 슬래시(/)는 의미가 전혀 없고 혼란을 야기할 수 있다.
URI내의 모든 문자는 리소스의 고유 ID에 포함된다. URI 가 다르면 리소스도 다르기 때문에 명확한 URI를 생성해야한다.
❌http://dev-cool.tistory.com/users/
⭕http://dev-cool.tistory.com/users
- 행위를 포함하지 않는다.
행위는 URI 대신 Method를 사용하여 전달한다.
❌ POSThttp://dev-cool.tistory.com/users/post/1
⭕ PUThttp://dev-cool.tistory.com/users/1
- 파일 확장자는 URL에 포함시키지 않는다.
파일 확장자는 URI에 포함하지 말아야한다. 대신 Content-Type 헤더를 통해 전달되는대로 미디어 타입을 사용하여 body 콘텐츠를 처리하는 방법을 결정한다.
Rest API 클라이언트는 HTTP에서 제공하는 형식 선택 메커니즘인 Accept 요청 헤더를 활용하도록 권장해야한다.
❌ <http://dev-cool.tistory.com/users/photo.jpg>
⭕ GET <http://dev-cool.tistory.com/users/photo>
HTTP/1.1 Host: dev-cool.tistory.com Accept: image/jpg
const fetchPhoto = async () => {
try {
const response = await fetch('<http://dev-cool.tistory.com/users/photo>', {
method: 'GET',
headers: {
'Accept': 'image/jpg' // Requesting image in JPG format using Accept header
}
});
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
const blob = await response.blob();
const imgUrl = URL.createObjectURL(blob);
// Creating an image element to display the fetched photo
const imgElement = document.createElement('img');
imgElement.src = imgUrl;
document.body.appendChild(imgElement);
} catch (error) {
console.error('Failed to fetch the photo:', error);
}
};
fetchPhoto();
- 전달하고자 하는 명사를 사용하되, 컨트롤 자원을 의미하는 경우 예외적으로 동사를 사용한다.
❌http://dev-cool.tistory.com/course/writing
⭕http://dev-cool.tistory.com/course/write
- URI에 작성되는 영어를 복수형으로 작성한다.
하나의 인스턴스를 복수형으로 표시하는게 문법적으로 맞지 않겠다고 생각할수 있지만 자원은 데이터의 집합을 나타내기 때문에, 리소스 컬렉션을 표현하는 URI는 복수형으로 작성하는 것이 자연스럽다.
⭕http://api.college.com/students/3248234/courses