GET → 데이터를 가져올 때 사용POST → 데이터를 서버에 보낼 때 사용PUT → 기존 데이터를 수정할 때 사용DELETE → 데이터를 삭제할 때 사용npm install axios 또는 yarn add axios로 설치 후 사용 가능합니다. Axios 기본 사용 예제
import axios from 'axios';
axios.get('https://jsonplaceholder.typicode.com/posts/1')
.then(response => console.log(response.data))
.catch(error => console.error('Error:', error));
window.fetch() 메서드를 사용하여 서버와 데이터를 주고받을 수 있습니다. async/await 문법과 함께 사용할 수 있습니다. Fetch 기본 사용 예제
fetch('https://jsonplaceholder.typicode.com/posts/1')
.then(response => response.json()) // JSON 변환
.then(data => console.log(data))
.catch(error => console.error('Error:', error));
| 비교 항목 | Fetch | Axios |
|---|---|---|
| 지원 여부 | 브라우저 내장 API | 별도 설치 필요 (npm install axios) |
| 기본 제공 기능 | 최소한의 기능 제공 | JSON 자동 변환, 응답 타임아웃, 요청 취소 기능 제공 |
| JSON 변환 | response.json()을 수동 호출해야 함 | 자동으로 response.data에 JSON 변환됨 |
| 에러 처리 | HTTP 400~500 에러는 catch에서 감지되지 않음 | 모든 요청 오류를 catch에서 감지 가능 |
| 요청 취소 기능 | 기본적으로 제공되지 않음 | axios.CancelToken을 사용하여 요청 취소 가능 |
| 타임아웃 설정 | 직접 구현해야 함 | timeout 옵션 제공 |
| 인터셉터 기능 | 없음 | request, response 인터셉터 제공 |
async/await과 함께 사용 가능 response.json()) response.data로 바로 사용 가능 catch에서 감지 가능 CancelToken을 사용하여 요청 취소 가능 npm install axios) Fetch가 적합한 경우
Axios가 적합한 경우