나의 발자취

RESTful API 설계 후 CURL로 데이터 주고받기 본문

Backend

RESTful API 설계 후 CURL로 데이터 주고받기

달모드 2024. 10. 14. 12:28

 

POST /posts - 게시글 쓰기

아래와 같이 api 기본 틀을 설계해준다. (깃허브에 커밋 이력 참고)
https://github.com/est22/backend/commit/5056afee59d19a4ff809b78190a2904ee10f17d8)

 

1. curl -X POST -H "Content-Type: application/json" -d '{"title":"Test1", "content":"test content", "author":"lia"}' http://localhost:3000/posts

를 입력하고 엔터를 치면, 아래와 같이 line 73에 입력한 대로 결과가 나온다.

 

2. curl http://localhost:3000/posts로 GET 요청을 하면, 현재 존재하는 데이터들을 json 객체로 받아보게 된다.

 

 

PUT /posts/:id - 게시글 수정

 

위와 같이 코드를 작성하고, curl로 수정할 내용을 PUT으로 요청해준다.

curl -X PUT -H "Content-Type: application/json" -d '{"title":"Test2", "content":"test content edited", "author":"lia"}' http://localhost:3000/posts/2

그리고, 수정된 내용을 확인해보는 GET 요청을 넣어본다.

curl http://localhost:3000/posts/2

 

실행화면

 

 

DELETE /posts/:id - 게시글 삭제

 

실행화면

아래처럼 아무것도 안나오면 성공이다.

 

 

728x90
반응형
Comments