이번 강의(21강)는 기능 추가가 아니라 API 설계를 REST답게 수정하는 단계였다.
기존에는 개발 편의를 위해 삭제를 GET 요청으로 처리했지만,
이번 강의에서는 HTTP Method의 의미에 맞게 DELETE로 변경했다.
그리고 강의 말미에 자연스럽게 이런 질문이 떠올랐다.
❓ “그럼 React 같은 프론트에서 DELETE 요청은 호출할 수 있나?”
이 글에서는 그 질문까지 같이 정리한다.
기존 삭제 방식은 이런 형태였다.
GET /api/v1/posts/{id}/delete
GET /api/v1/posts/{postId}/comments/{id}/delete
GET으로 삭제하면 URL만 보고는
GET 요청은 브라우저, 프록시, 크롤러 등에 의해
👉 GET으로 삭제하면 의도치 않은 삭제 사고가 발생할 수 있다.
GET /api/v1/posts/{id}/delete
DELETE /api/v1/posts/{id}
@DeleteMapping("/{id}")
@Transactional
public RsData<Void> delete(@PathVariable int id) {
Post post = postService.findById(id).get();
postService.delete(post);
return new RsData<>(
"200-1",
"%d번 글이 삭제되었습니다.".formatted(id)
);
}
GET /api/v1/posts/{postId}/comments/{id}/delete
DELETE /api/v1/posts/{postId}/comments/{id}
@DeleteMapping("/{id}")
@Transactional
public RsData<Void> delete(
@PathVariable int postId,
@PathVariable int id
) {
Post post = postService.findById(postId).get();
PostComment postComment = post.findCommentById(id).get();
postService.deleteComment(post, postComment);
return new RsData<>(
"200-1",
"%d번 댓글이 삭제되었습니다.".formatted(id)
);
}
posts/{postId}/comments/{id}
결론부터 말하면:
된다. 완전 가능하다.
fetch("http://localhost:8080/api/v1/posts/1", {
method: "DELETE",
});
axios.delete("http://localhost:8080/api/v1/posts/1");
둘 다 정상적으로 DELETE 요청이 나간다.
보통 아래 3가지가 섞여서 오해가 생긴다.
프론트(3000) → 백엔드(8080) 요청 시
DELETE / POST / PUT 은 프리플라이트(OPTIONS) 요청이 발생한다.
백엔드에 CORS 설정이 없으면:
👉 이건 DELETE가 안 되는 게 아니라
👉 CORS 설정이 없어서 막힌 것
Spring Security 기본 설정에서는:
그래서 토큰 없으면 DELETE가 차단된다.
👉 이 경우도 DELETE가 안 되는 게 아니라 보안 설정 문제
HTML <form> 태그는
이걸 보고
“DELETE는 브라우저에서 못 보낸다”
라고 오해하는 경우가 많다.
하지만 React는:
👉 DELETE 사용 가능
REST API에서 삭제는 GET이 아니라 DELETE,
그리고 React에서도 DELETE 요청은 당연히 가능하다.