Fetch์ Axios๋?
1. Fetch์ Axios ์ ์
`fetch`์ `axios`๋ JavaScript์์ HTTP ์์ฒญ์ ๋ณด๋ด๋ ๋ฐ ์ฌ์ฉ๋๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ๋ฐ API์ด๋ค.
๋ ๋ค ํด๋ผ์ด์ธํธ์์ ์๋ฒ๋ก ๋ฐ์ดํฐ๋ฅผ ์์ฒญํ๊ณ ์๋ต์ ๋ฐ๋ ๋ฐ ์ฌ์ฉ๋๋ฉฐ, RESTful API์ ํจ๊ป ์์ฃผ ํ์ฉ๋๋ค.
โ Fetch vs Axios ๋น๊ต
๋น๊ต ํญ๋ชฉ | Fetch API | Axios |
๊ธฐ๋ณธ ์ ๊ณต ์ฌ๋ถ | ๋ธ๋ผ์ฐ์ ๋ด์ฅ API | ์ธ๋ถ ๋ผ์ด๋ธ๋ฌ๋ฆฌ (์ค์น ํ์) |
์ฌ์ฉ๋ฒ | `.then()` ๋๋ `async/await` ์ฌ์ฉ | `.then()` ๋๋ `async/await` ์ฌ์ฉ |
์๋ต ์ฒ๋ฆฌ | `Response` ๊ฐ์ฒด๋ฅผ ๋ช ์์ ์ผ๋ก `.json()`์ผ๋ก ๋ณํํด์ผ ํจ | JSON ์๋ ๋ณํ |
์ค๋ฅ ์ฒ๋ฆฌ | HTTP ์ํ ์ฝ๋ ์ค๋ฅ๋ฅผ ์ง์ ์ฒ๋ฆฌํด์ผ ํจ | ์๋์ผ๋ก ์ค๋ฅ๋ฅผ throw |
์์ฒญ ์ทจ์ | ์ง์ ์ ํจ | `AbortControlle`r ๋๋ `axios.CancelToken` ์ง์ |
์์ฒญ ๋ฐ ์๋ต ๋ณํ | ์ค์ ์ด ๋ฒ๊ฑฐ๋ก์ | ์ธํฐ์ ํฐ ๊ธฐ๋ฅ ์ ๊ณต |
๋ธ๋ผ์ฐ์ ํธํ์ฑ | ๋๋ถ๋ถ์ ์ต์ ๋ธ๋ผ์ฐ์ ์์ ์ง์ | IE์ ๊ฐ์ ๊ตฌํ ๋ธ๋ผ์ฐ์ ์์๋ polyfill์ ์ฌ์ฉํ๋ฉด ์ง์ ๊ฐ๋ฅ |
2. Fetch API
`fetch()`๋ Promise ๊ธฐ๋ฐ์ HTTP ์์ฒญ API๋ก, ๋ธ๋ผ์ฐ์ ์์ ๊ธฐ๋ณธ์ ์ผ๋ก ์ ๊ณต๋๋ค.
์ฃผ๋ก RESTful API ์์ฒญ์ ๋ณด๋ผ ๋ ์ฌ์ฉ๋๋ค.
1๏ธโฃ GET ์์ฒญ
fetch("https://jsonplaceholder.typicode.com/posts/1")
.then(response => response.json()) // JSON ๋ณํ
.then(data => console.log(data))
.catch(error => console.error("Error:", error));
- `fetch()`๋ ๊ธฐ๋ณธ์ ์ผ๋ก GET ์์ฒญ
- `response.json()`์ ํธ์ถํด์ผ JSON ๋ฐ์ดํฐ๋ฅผ ์ฌ์ฉํ ์ ์์
- `.catch()`๋ก ๋คํธ์ํฌ ์ค๋ฅ๋ฅผ ์ฒ๋ฆฌํ์ง๋ง, HTTP ์ํ ์ค๋ฅ(์: 404)๋ ์๋์ผ๋ก throw๋์ง ์์
2๏ธโฃ POST ์์ฒญ
fetch("https://jsonplaceholder.typicode.com/posts", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
title: "New Post",
body: "This is a new post",
userId: 1
})
})
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error("Error:", error));
- `method`: HTTP ์์ฒญ ๋ฉ์๋ ์ง์ (`POST`, `PUT`, `DELETE` ๋ฑ)
- `headers`: `Content-Type`์ ๋ช ์ํด์ผ JSON ๋ฐ์ดํฐ ์ ์ก ๊ฐ๋ฅ
- `body`: JSON ๋ฐ์ดํฐ๋ฅผ ๋ฌธ์์ด(`stringify`)๋ก ๋ณํํ์ฌ ์ ๋ฌ
3๏ธโฃ ์ค๋ฅ ์ฒ๋ฆฌ (HTTP ์ํ ์ฝ๋ ์ฒดํฌ)
fetch("https://jsonplaceholder.typicode.com/posts/invalid")
.then(response => {
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
return response.json();
})
.then(data => console.log(data))
.catch(error => console.error("Error:", error));
- `response.ok`๋ฅผ ์ฒดํฌํด์ผ HTTP ์ค๋ฅ(404, 500 ๋ฑ)๋ ๊ฐ์ง ๊ฐ๋ฅ
4๏ธโฃ ์์ฒญ ์ทจ์ (`AbortController`)
const controller = new AbortController();
const signal = controller.signal;
fetch("https://jsonplaceholder.typicode.com/posts", { signal })
.then(response => response.json())
.then(data => console.log(data))
.catch(error => {
if (error.name === "AbortError") {
console.error("Fetch aborted!");
}
});
controller.abort(); // ์์ฒญ ์ทจ์
- `AbortController`๋ฅผ ์ฌ์ฉํ๋ฉด ์์ฒญ์ ์ค๋จ ๊ฐ๋ฅ
- ํน์ ์ํฉ์์ ๋คํธ์ํฌ ์์ฒญ์ ์ทจ์ํ ๋ ์ ์ฉ
3. Axios
`axios`๋ Fetch API๋ณด๋ค ๋ ๊ฐ๊ฒฐํ ์ฝ๋ ์์ฑ ๋ฐ ๋ค์ํ ๊ธฐ๋ฅ ์ ๊ณต์ด ๊ฐ๋ฅํ HTTP ํด๋ผ์ด์ธํธ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ด๋ค.
`npm` ๋๋ `yarn`์ ํตํด ์ค์น ํ ์ฌ์ฉ ๊ฐ๋ฅํ๋ค.
# sh
npm install axios
1๏ธโฃ `GET` ์์ฒญ
import axios from "axios";
axios.get("https://jsonplaceholder.typicode.com/posts/1")
.then(response => console.log(response.data))
.catch(error => console.error("Error:", error));
- Fetch์ ๋ฌ๋ฆฌ `response.data`์ JSON ๋ฐ์ดํฐ๊ฐ ์๋์ผ๋ก ๋ค์ด๊ฐ
- HTTP ์ค๋ฅ ์ํ ์ฝ๋๋ ์๋์ผ๋ก ์์ธ ๋ฐ์
2๏ธโฃ `POST` ์์ฒญ
axios.post("https://jsonplaceholder.typicode.com/posts", {
title: "New Post",
body: "This is a new post",
userId: 1
})
.then(response => console.log(response.data))
.catch(error => console.error("Error:", error));
- `headers` ์์ด๋ JSON ๋ฐ์ดํฐ๋ฅผ ์๋ ๋ณํํ์ฌ ์ ์ก ๊ฐ๋ฅ
3๏ธโฃ ์์ฒญ๊ณผ ์๋ต ๋ณํ (์ธํฐ์ ํฐ)
Axios๋ ์์ฒญ๊ณผ ์๋ต์ ์ค๊ฐ์์ ๋ณํํ ์ ์๋ ์ธํฐ์ ํฐ ๊ธฐ๋ฅ์ ์ ๊ณตํ๋ค.
axios.interceptors.request.use(config => {
config.headers.Authorization = "Bearer my-jwt-token"; // ์์ฒญ ํค๋ ์ถ๊ฐ
return config;
});
- ๋ชจ๋ ์์ฒญ์ ์๋์ผ๋ก ํ ํฐ ์ถ๊ฐ ๊ฐ๋ฅ
- Fetch API๋ก ๊ตฌํํ๋ ค๋ฉด ๋งค๋ฒ `headers`๋ฅผ ์ค์ ํด์ผ ํจ
4๏ธโฃ ์์ฒญ ์ทจ์ (`CancelToken`)
const source = axios.CancelToken.source();
axios.get("https://jsonplaceholder.typicode.com/posts", {
cancelToken: source.token
})
.then(response => console.log(response.data))
.catch(error => {
if (axios.isCancel(error)) {
console.error("Request canceled:", error.message);
}
});
source.cancel("Request aborted manually.");
- `Axios`๋ ๋คํธ์ํฌ ์์ฒญ์ ์ค๋จํ ์ ์๋ ๊ธฐ๋ฅ์ ๊ธฐ๋ณธ ์ ๊ณต
4. Fetch vs Axios ์ ๋ฆฌ
๋น๊ต ํญ๋ชฉ | Fetch API | Axios |
JSON ์๋ ๋ณํ | โ (`response.json()` ํ์) | โ (`response.data`๋ก ๋ฐ๋ก ์ ๊ทผ ๊ฐ๋ฅ) |
์ค๋ฅ ์ฒ๋ฆฌ | โ (HTTP ์ค๋ฅ๋ ์ง์ ์ฒ๋ฆฌ) | โ (์๋์ผ๋ก ์ค๋ฅ throw) |
์์ฒญ ์ทจ์ | โ (`AbortController` ์ฌ์ฉ) | โ (`CancelToken` ์ฌ์ฉ) |
์์ฒญ & ์๋ต ๋ณํ | โ (๋งค๋ฒ ์๋ ์ฒ๋ฆฌ) | โ (์ธํฐ์ ํฐ ๊ธฐ๋ฅ ์ ๊ณต) |
์์ฒญ ๋ฐ๋ณต ์คํ | โ (๋ฐ๋ณต ํธ์ถ ํ์) | โ (์๋ ์ฌ์๋ ์ค์ ๊ฐ๋ฅ) |
๋ธ๋ผ์ฐ์ ํธํ์ฑ | ์ต์ ๋ธ๋ผ์ฐ์ ์ง์ | ๊ตฌํ ๋ธ๋ผ์ฐ์ ๊น์ง ์ง์ |
Fetch์ Axios ์ ๋ฆฌ:
1. Fetch๋ ๊ธฐ๋ณธ์ ์ผ๋ก ๋ด์ฅ๋ API๋ก, ๊ฐ๋จํ ์์ฒญ์ ์ฒ๋ฆฌํ ๋ ์ ํฉํ๋ค~
2. Axios๋ JSON ๋ณํ, ์ค๋ฅ ์ฒ๋ฆฌ, ์ธํฐ์ ํฐ ๊ธฐ๋ฅ ๋ฑ ์ถ๊ฐ ๊ธฐ๋ฅ์ด ๋ง์ ์ฌ์ฉ์ด ๋ ํธ๋ฆฌํ๋ค~
3. ์์ฒญ์ ์ค๋จํ๊ฑฐ๋, ์ธ์ฆ ํ ํฐ์ ์๋์ผ๋ก ์ถ๊ฐํ๋ ๊ธฐ๋ฅ์ด ํ์ํ๋ฉด Axios๋ฅผ ์ฌ์ฉํ๋ ๊ฒ์ด ์ ๋ฆฌํ๋ค~
4. ์ต์ ๋ธ๋ผ์ฐ์ ํ๊ฒฝ์์๋ Fetch๊ฐ ๊ฐ๋ณ๊ณ ๋น ๋ฅธ ๋์์ด ๋ ์๋ ์๋ค~