Promise API를 활용하는 HTTP 비동기 통신 라이브러리
npm install axios
import axios from 'axios';
// GET 요청
axios.get('https://jsonplaceholder.typicode.com/todos/1')
.then(response => {
console.log(response.data);
})
.catch(error => {
console.error(error);
});
// POST 요청
axios.post('https://jsonplaceholder.typicode.com/todos', {
userId: 1,
title: 'New Todo',
completed: false
})
.then(response => {
console.log(response.data);
})
.catch(error => {
console.error(error);
});
// PATCH 요청
axios.patch('https://jsonplaceholder.typicode.com/todos/1', {
completed: true
})
.then(response => {
console.log(response.data);
})
.catch(error => {
console.error(error);
});
// DELETE 요청
axios.delete('https://jsonplaceholder.typicode.com/todos/1')
.then(response => {
console.log(response.data);
})
.catch(error => {
console.error(error);
});
API 관련해서 계속해서 공부해야지 하다가
프로젝트를 진행하며 적용시켜보면서 공부할 수 있는 기회가 되었다.
그동안은 그냥 공식처럼 사용했던 거 같은데 이번 기회에 제대로 동작하는 방식을 제대로 이해할 수 있는 기회가 되어 다행이다..