๋ธ๋ผ์ฐ์ ์ ์๋ฒ ๊ฐ์ HTTP ์์ฒญ์ ๋ณด๋ด๊ธฐ ์ํ ์๋ฐ์คํฌ๋ฆฝํธ ๋ผ์ด๋ธ๋ฌ๋ฆฌ
์ฝ๊ฒ ๋งํด: ํ๋ก ํธ์๋ โ ๋ฐฑ์๋(API ์๋ฒ) ๋ก ๋ฐ์ดํฐ๋ฅผ ๋ณด๋ด๊ฑฐ๋ ๋ฐ์์ฌ ๋ ์ฐ๋ ๋๊ตฌ์ด๋ค.
๋๊ฐ ๋ชจ๋ HTTP์์ฒญ์ ๋ณด๋ด๊ธฐ ์ํ ๋๊ตฌ์ด๋ค. ๊ทธ๋ ๋ค๋ฉด ๋๊ฐ์ ์ฐจ์ด์ ์ด ๋ฌด์์ผ๊น?
| ํญ๋ชฉ | Axios | fetch() |
|---|---|---|
| ์ง์ ์ฌ๋ถ | ์ธ๋ถ ๋ผ์ด๋ธ๋ฌ๋ฆฌ (์ค์น ํ์) | ๋ธ๋ผ์ฐ์ ๊ธฐ๋ณธ ๋ด์ฅ |
| ๋ฌธ๋ฒ ๊ฐ๊ฒฐ์ฑ | ๋ ๊ฐ๋จํ๊ณ ์ง๊ด์ | ๋ค์ ๋ณต์ก (ํนํ ์๋ฌ/JSON ์ฒ๋ฆฌ) |
| ์๋ต JSON ์๋ ์ฒ๋ฆฌ | ์๋ ํ์ฑ (res.data) | ์๋์ผ๋ก .json() ํธ์ถ ํ์ |
| ์๋ฌ ์ฒ๋ฆฌ | HTTP ์ค๋ฅ๋ .catch()๋ก ์กํ | 200~299 ์ธ ์๋ฌ๋ ์ง์ ์ฒ๋ฆฌํด์ผ ํจ |
| ์์ฒญ/์๋ต ์ธํฐ์ ํฐ | ๊ฐ๋ฅ | ๋ถ๊ฐ๋ฅ |
| ์์ฒญ ์ทจ์ | CancelToken ์ง์ | ์ง์ AbortController ์จ์ผ ํจ |
| ๋ธ๋ผ์ฐ์ ์ง์ | IE ํฌํจ ๋๋ถ๋ถ ๋ธ๋ผ์ฐ์ | IE ๋ฏธ์ง์ |
| ํค๋ ์ค์ | ์ฌ์ | ์๋์ ์ผ๋ก ๋ณต์กํจ |
fetch() ์ฌ์ฉ ์์
// fetch๋ ๋ธ๋ผ์ฐ์ ์ ๊ธฐ๋ณธ ๋ด์ฅ๋ ํจ์๋ก, URL์ HTTP ์์ฒญ์ ๋ณด๋ธ๋ค fetch("https://jsonplaceholder.typicode.com/users") .then((res) => { // res.ok๋ HTTP ์ํ์ฝ๋๊ฐ 200~299 ๋ฒ์์ผ ๋ true์ด๋ค if (!res.ok) { throw new Error("HTTP ์ค๋ฅ: " + res.status); // ์๋์ผ๋ก ์๋ฌ ์ฒดํฌ ํ์ } return res.json(); // ์๋ต ๋ณธ๋ฌธ์ JSON์ผ๋ก ๋ณํ (๋น๋๊ธฐ ์์ ) }) .then((data) => { // ์์์ ํ์ฑํ JSON ๋ฐ์ดํฐ๋ฅผ ๋ฐ์์ ์ฌ์ฉ console.log("์ ์ ๋ฐ์ดํฐ (fetch):", data); }) .catch((err) => { // ๋คํธ์ํฌ ์ค๋ฅ ๋๋ ์์์ throwํ ์๋ฌ๊ฐ catch๋ก ์กํ console.error("์๋ฌ ๋ฐ์ (fetch):", err); });
Axios ์ฌ์ฉ ์์
// axios๋ ์ธ๋ถ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ก, HTTP ์์ฒญ์ ๋ ๊ฐ๊ฒฐํ๊ณ ๊ฐ๋ ฅํ๊ฒ ์ฒ๋ฆฌํ ์ ์๋ค import axios from 'axios'; axios.get("https://jsonplaceholder.typicode.com/users") .then((res) => { // res.data๋ ์ด๋ฏธ JSON์ผ๋ก ์๋ ํ์ฑ๋ ๋ฐ์ดํฐ์ ๋๋ค console.log("์ ์ ๋ฐ์ดํฐ (axios):", res.data); }) .catch((err) => { // ๋คํธ์ํฌ ์ค๋ฅ๋ฟ ์๋๋ผ HTTP ์ํ์ฝ๋ ์๋ฌ(404, 500 ๋ฑ)๋ ์๋์ผ๋ก catch๋จ console.error("์๋ฌ ๋ฐ์ (axios):", err); });
pnpm add axios
๋๋
npm install axios
| ๋ฉ์๋ | ์ค๋ช |
|---|---|
axios.get(url) | ๋ฐ์ดํฐ ์กฐํ |
axios.post(url, data) | ๋ฐ์ดํฐ ์ ์ก |
axios.put(url, data) | ์ ์ฒด ์์ |
axios.patch(url, data) | ์ผ๋ถ ์์ |
axios.delete(url) | ๋ฐ์ดํฐ ์ญ์ |
axios.get(url)์๋ฒ๋ก๋ถํฐ ๋ฐ์ดํฐ๋ฅผ "์กฐํ(GET)" ํ ๋ ์ฌ์ฉ
์ฌ์ฉ ์ํฉ) ๊ฒ์๊ธ ๋ชฉ๋ก ์กฐํ, ํน์ ์ ์ ์ ๋ณด ๊ฐ์ ธ์ค๊ธฐ
import axios from 'axios';
// ์๋ฒ์์ ์ฌ์ฉ์ ๋ชฉ๋ก์ ์กฐํ
axios.get('https://jsonplaceholder.typicode.com/users')
.then((res) => {
// res.data์๋ JSON ํํ์ ์ ์ ๋ฐฐ์ด์ด ๋ด๊ฒจ ์์
console.log('์ ์ ๋ฆฌ์คํธ:', res.data);
})
.catch((err) => {
// ๋คํธ์ํฌ ๋ฌธ์ ๋ HTTP ์ค๋ฅ ๋ฐ์ ์ ์คํ๋จ
console.error('์กฐํ ์คํจ:', err);
});
axios.post(url, data)์๋ฒ์ ์๋ก์ด ๋ฐ์ดํฐ๋ฅผ "์์ฑ(POST)" ํ ๋ ์ฌ์ฉ
์ฌ์ฉ ์ํฉ) ํ์๊ฐ์
, ๋ก๊ทธ์ธ, ๋๊ธ/๊ฒ์๊ธ ์์ฑ
// ์๋ฒ์ ์ ๊ฒ์๊ธ ๋ฐ์ดํฐ๋ฅผ ์ ์กํ์ฌ ์์ฑ ์์ฒญ
axios.post('https://jsonplaceholder.typicode.com/posts', {
title: '์ ๊ฒ์๊ธ ์ ๋ชฉ', // ๊ฒ์๊ธ ์ ๋ชฉ
body: '๊ฒ์๊ธ ๋ด์ฉ์
๋๋ค.', // ๊ฒ์๊ธ ๋ณธ๋ฌธ
userId: 1 // ์์ฑ์ ID
})
.then((res) => {
// ์์ฑ๋ ๊ฒ์๊ธ ๋ฐ์ดํฐ๊ฐ res.data์ ๋ด๊ฒจ์ ๋์์ด
console.log('๊ฒ์๊ธ ์์ฑ ๊ฒฐ๊ณผ:', res.data);
})
.catch((err) => {
// ์์ฒญ ์คํจ ๋๋ ์ค๋ฅ ๋ฐ์
console.error('๊ฒ์๊ธ ์์ฑ ์คํจ:', err);
});
axios.put(url, data)ํน์ ๋ฐ์ดํฐ๋ฅผ "์์ ํ ๋ฎ์ด์์ฐ๋" ์์ (์ ์ฒด ์
๋ฐ์ดํธ) ์ ์ฌ์ฉ
์ฌ์ฉ ์ํฉ) ๋ง์ดํ์ด์ง ์ ๋ณด ์ ์ฒด ์์ , ๊ฒ์๊ธ ํต์งธ๋ก ๋ฎ์ด์ฐ๊ธฐ
// ๊ธฐ์กด ๊ฒ์๊ธ ์ ์ฒด๋ฅผ ์ ๋ฐ์ดํฐ๋ก ๋ฎ์ด์ฐ๊ธฐ (๋ชจ๋ ํ๋๋ฅผ ๋ค์ ๋ณด๋ด์ผ ํจ)
axios.put('https://jsonplaceholder.typicode.com/posts/1', {
title: '์ ์ฒด ์์ ๋ ์ ๋ชฉ',
body: '์ ์ฒด ์์ ๋ ๋ด์ฉ',
userId: 1
})
.then((res) => {
// ์์ ๋ ๊ฒ์๊ธ ๋ฐ์ดํฐ ๋ฐํ
console.log('์ ์ฒด ์์ ๊ฒฐ๊ณผ:', res.data);
})
.catch((err) => {
console.error('์ ์ฒด ์์ ์คํจ:', err);
});
axios.patch(url, data)ํน์ ๋ฐ์ดํฐ์ "์ผ๋ถ๋ถ๋ง" ์์ ํ ๋ ์ฌ์ฉ
์ฌ์ฉ ์ํฉ) ๊ฒ์๊ธ ์ ๋ชฉ๋ง ์์ , ํ๋กํ ์ด๋ฏธ์ง ํ ๊ฐ์ง๋ง ๋ณ๊ฒฝ
// ๊ธฐ์กด ๊ฒ์๊ธ์ ์ผ๋ถ๋ง ์์ (์ฌ๊ธฐ์ ์ ๋ชฉ๋ง ์์ )
axios.patch('https://jsonplaceholder.typicode.com/posts/1', {
title: '๋ถ๋ถ๋ง ์์ ๋ ์ ๋ชฉ'
})
.then((res) => {
// ๋ถ๋ถ๋ง ์์ ๋ ๊ฒฐ๊ณผ ๋ฐ์ดํฐ ๋ฐํ
console.log('๋ถ๋ถ ์์ ๊ฒฐ๊ณผ:', res.data);
})
.catch((err) => {
console.error('๋ถ๋ถ ์์ ์คํจ:', err);
});
axios.delete(url)ํน์ ๋ฐ์ดํฐ๋ฅผ "์ญ์ (DELETE)" ํ ๋ ์ฌ์ฉ
์ฌ์ฉ ์ํฉ) ๊ฒ์๊ธ ์ญ์ , ๋๊ธ ์ญ์ , ๊ณ์ ํํด
// ํน์ ID๋ฅผ ๊ฐ์ง ๊ฒ์๊ธ์ ์ญ์ ์์ฒญ
axios.delete('https://jsonplaceholder.typicode.com/posts/1')
.then((res) => {
// ์ญ์ ๋ ๋ณดํต ๋ด์ฉ ์์ด ์ํ ์ฝ๋๋ง ๋์์ด (204 No Content)
console.log('์ญ์ ์๋ฃ, ์ํ์ฝ๋:', res.status);
})
.catch((err) => {
console.error('์ญ์ ์คํจ:', err);
});