( 참조: https://poiemaweb.com/js-rest-api)
# bad
GET /getTodos/1
GET /todos/show/1
/ / show나 get등을 표현 x
# good
GET /todos/1
# bad
GET /todos/delete/1
# good
DELETE /todos/1
GET: 모든/특정 리소스를 조회 (페이로드 X)
POST: 리소스 생성 (페이로드 O)
PUT: 리소스의 전체를 교체 (페이로드O)
PATCH: 리소스의 일부를 수정 (페이로드O)
DELETE: 모든/특정 리소스를 삭제 (페이로드 X)
| 구성 요소 | 내용 | 표현 방법 |
|---|---|---|
| Resource | 자원 | HTTP URI |
| Verb | 자원에 대한 행위 | HTTP Method |
| Representations | 자원에 대한 행위의 내용 | HTTP Message Pay Load |
$ mkdir rest-api-exam && cd rest-api-exam
$ npm init -y
$ npm install json-server
db.json
{
"todos": [
{ "id": 1, "content": "HTML", "completed": false },
{ "id": 2, "content": "CSS", "completed": true },
{ "id": 3, "content": "Javascript", "completed": false }
]
}
npm script를 사용하여 json-server를 실행한다. 아래와 같이 package.json을 수정한다.
{
"name": "rest-api-exam",
"version": "1.0.0",
"description": "",
"scripts": {
"start": "json-server --watch db.json --port 5000"
},
"dependencies": {
"json-server": "^0.15.0"
}
}
서버측
const xhr = new XMLHttpRequest();
xhr.open('GET', 'http://localhost:5000/todos');
xhr.send();
xhr.onreadystatechange = function (e) {
if (xhr.readyState !== XMLHttpRequest.DONE) return;
if(xhr.status === 200) { // 200: OK => https://httpstatuses.com
console.log(xhr.responseText);
} else {
console.log("Error!");
}
};
클라이언트 (터미널)
$ curl -X GET http://localhost:5000/todos
// 전체 조회

$ curl -X GET http://localhost:5000/todos/1
// 특정 번호 조회

서버측
const xhr = new XMLHttpRequest();
xhr.open('POST', 'http://localhost:5000/todos');
xhr.setRequestHeader('Content-type', 'application/json');
xhr.send(JSON.stringify({ id: 4, content: 'Angular', completed: true }));
xhr.onreadystatechange = function (e) {
if (xhr.readyState !== XMLHttpRequest.DONE) return;
if(xhr.status === 201) { // 201: Created
console.log(xhr.responseText);
} else {
console.log("Error!");
}
};
클라이언트 (터미널)
$ curl -X POST http://localhost:5000/todos -H "Content-Type:
application/json" -d '{"id": 4, "content": "Angular", "completed": true}'

서버측
const xhr = new XMLHttpRequest();
xhr.open('PUT', 'http://localhost:5000/todos/4');
xhr.setRequestHeader('Content-type', 'application/json');
xhr.send(JSON.stringify({ id: 4, content: 'React', completed: false }));
xhr.onreadystatechange = function (e) {
if (xhr.readyState !== XMLHttpRequest.DONE) return;
if(xhr.status === 200) {
console.log(xhr.responseText);
} else {
console.log("Error!");
}
};
클라이언트 (터미널)
$ curl -X PUT http://localhost:5000/todos/4 -H "Content-Type: application/json" -d '{"id": 4, "content": "React", "completed": false}'
서버측
const xhr = new XMLHttpRequest();
xhr.open('PATCH', 'http://localhost:5000/todos/4');
xhr.setRequestHeader('Content-type', 'application/json');
xhr.send(JSON.stringify({ completed: true }));
xhr.onreadystatechange = function (e) {
if (xhr.readyState !== XMLHttpRequest.DONE) return;
if(xhr.status === 200) {
console.log(xhr.responseText);
} else {
console.log("Error!");
}
};
클라이언트 (터미널)
$ curl -X PATCH http://localhost:5000/todos/4 -H "Content-Type: application/json" -d '{"completed": true}'
서버측
const xhr = new XMLHttpRequest();
xhr.open('DELETE', 'http://localhost:5000/todos/4');
xhr.send();
xhr.onreadystatechange = function (e) {
if (xhr.readyState !== XMLHttpRequest.DONE) return;
if(xhr.status === 200) {
console.log(xhr.responseText);
} else {
console.log("Error!");
}
};
클라이언트 (터미널)
$ curl -X DELETE http://localhost:5000/todos/4
-crul을 통해서 http 요청 테스트, API 테스트 및 디버깅, 파일 다운로드, HTTP 메서드 변경, HTTP 요청에 데이터 추가, HTTP 헤더 추가, FTP,SCP 등 다양한 프로토콜 사용 등등 많은 것을 할 수 있다.
curl -X POST http://example.com/api/resource
curl -H "Content-Type: application/json" http://example.com/api/resource
curl -X POST -H "Content-Type: application/json" -d '{"id": 4, "content": "Angular", "completed": true}' http://example.com/api/resource
이러한 CURL 명령어의 옵션들을 적절히 조합하여 다양한 HTTP 요청을 생성할 수 있다.