[JS] ๐Ÿ“ก fetch API, Axios ์‚ฌ์šฉ๋ฒ•

TATAยท2023๋…„ 1์›” 19์ผ
0

JavaScript

๋ชฉ๋ก ๋ณด๊ธฐ
22/25

๋น„๋™๊ธฐ ์š”์ฒญ์˜ ๋Œ€ํ‘œ์ ์ธ ์‚ฌ๋ก€์—๋Š” ๋„คํŠธ์›Œํฌ ์š”์ฒญ์ด ์žˆ๋‹ค.
๊ทธ์ค‘์—์„œ URL๋กœ ์š”์ฒญํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๊ฐ€์žฅ ํ”ํ•œ๋ฐ,
URL๋กœ ์š”์ฒญํ•˜๋Š” ๊ฒƒ์„ ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•ด์ฃผ๋Š” API๊ฐ€ ๋ฐ”๋กœ fetch API์ด๋‹ค.

โ—๏ธ์ฐธ๊ณ ) fetch๋Š” Promise์˜ ๊ฐ์ฒด ํƒ€์ž…์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

โœ”๏ธ fetch API

๐Ÿ“ก ๊ธฐ๋ณธ์ ์ธ ๊ตฌ์กฐ

fetch('http://example.com/movies.json')
  .then((response) => response.json())
  .then((json) => console.log(json));
  .catch((error) => console.error(error));

๐Ÿ“ก ์˜ต์…˜ ์ œ๊ณต ๊ตฌ์กฐ

์„ ํƒ์ ์œผ๋กœ ๋‘ ๋ฒˆ์งธ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์ œ๊ณตํ•œ๋‹ค. (ํ•„์ˆ˜๊ฐ€ ์•„๋‹˜)

const data = { username: 'example' };

fetch('https://example.com/profile', {
  method: 'POST', // ๋˜๋Š” 'PUT', 'GET'
  headers: {
    'Content-Type': 'application/json',
  },
  body: JSON.stringify(data),
})
.then((response) => response.json())
.then((json) => {
  console.log('์„ฑ๊ณต:', json);
})
.catch((error) => {
  console.error('์‹คํŒจ:', error);
});

๐Ÿ“ก ๊ฐ„๋‹จํ•œ GET ์š”์ฒญ ์˜ˆ์‹œ

GET : ์ •๋ณด๋ฅผ ์š”์ฒญํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ๋˜๋Š” ๋ฉ”์„œ๋“œ

// Promise ver
fetch('https://koreanjson.com/users/1', { method: 'GET' })
  .then((response) => response.json())
  .then((json) => console.log(json))
  .catch((error) => console.log(error));

// Async / Await ver
async function request() {
  const response = await fetch('https://koreanjson.com/users/1', {
    method: 'GET',
  });
  const data = await response.json();
  console.log(data);
}

๐Ÿ“ก POST ์š”์ฒญ ์˜ˆ์‹œ

POST : ์„œ๋ฒ„์—๊ฒŒ ๋ฐ์ดํ„ฐ๋ฅผ ๋ณด๋‚ด๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ๋˜๋Š” ๋ฉ”์„œ๋“œ

// Promise ver
fetch('https://koreanjson.com/users', {
  method: 'POST',
  headers: {
    // JSON์˜ ํ˜•์‹์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ๋ณด๋‚ด์ค€๋‹ค๊ณ  ์„œ๋ฒ„์—๊ฒŒ ์•Œ๋ ค์ฃผ๋Š” ์—ญํ• ์ž…๋‹ˆ๋‹ค.
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({ nickName: 'ApeachIcetea', age: 20 }),
})
  .then((response) => response.json())
  .then((json) => console.log(json))
  .catch((error) => console.log(error));

// Async / Await ver
async function request() {
  const response = await fetch('https://koreanjson.com/users', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
    },
    body: JSON.stringify({ nickName: 'ApeachIcetea', age: 20 }),
  });
  const data = await response.json();
  console.log(data);
}

request();

โœ”๏ธ Axios

Axios๋Š” ๋ธŒ๋ผ์šฐ์ €, Node.js๋ฅผ ์œ„ํ•œ Promise API๋ฅผ ํ™œ์šฉํ•˜๋Š” HTTP ๋น„๋™๊ธฐ ํ†ต์‹  ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์ด๋‹ค.
Fetch API๋ณด๋‹ค ์‚ฌ์šฉ์ด ๊ฐ„ํŽธํ•˜๋ฉด์„œ ์ถ”๊ฐ€์ ์ธ ๊ธฐ๋Šฅ๋“ค์ด ํฌํ•จ๋˜์–ด์žˆ๋‹ค.
๊ทธ๋ž˜์„œ ์‹ค์ œ๋กœ๋Š” Fetch๋ณด๋‹ค Axios๋ฅผ ์ฃผ๋กœ ์‚ฌ์šฉํ•œ๋‹ค.

โ—๏ธ์ฐธ๊ณ ) HTTP๋ž€ 'HyperText Transfer Protocol'์˜ ์•ฝ์ž๋กœ ํ•˜์ดํผ ํ…์ŠคํŠธ๋ฅผ ๊ตํ™˜, ์ „์†ก์„ ์œ„ํ•œ ํ†ต์‹  ๊ทœ์•ฝ์ž…๋‹ˆ๋‹ค.

๐Ÿ“ก Axios ์„ค์น˜

npm install axios

//โ—๏ธ์ฐธ๊ณ ) ๊ธ€๋กœ๋ฒŒ ์„ค์น˜X, ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ์€ ํด๋”์— ๊ฐœ๋ณ„๋กœ ์„ค์น˜ํ•˜๋ฉด ๋œ๋‹ค.

๐Ÿ“ก ๊ธฐ๋ณธ์ ์ธ ๊ตฌ์กฐ

axios
  .get('https://koreanjson.com/users/1')
  .then((response) => {
    const { data } = response;
    console.log(data);
  })
  .catch((error) => console.log(error));

๐Ÿ“ก ์˜ต์…˜ ์ œ๊ณต ๊ตฌ์กฐ

์„ ํƒ์ ์œผ๋กœ ๋‘ ๋ฒˆ์งธ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์ œ๊ณตํ•œ๋‹ค. (ํ•„์ˆ˜๊ฐ€ ์•„๋‹˜)

const data = { username: 'example' };

axios
  .post('https://example.com/profile', data) // ๋˜๋Š” get, put
  .then((response) => {
    const { data } = response;
    console.log('์„ฑ๊ณต:', data);
  })
  .catch((error) => {
    console.error('์‹คํŒจ:', error);
  });

๐Ÿ“ก ๊ฐ„๋‹จํ•œ GET ์š”์ฒญ ์˜ˆ์‹œ

GET : ์ •๋ณด๋ฅผ ์š”์ฒญํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ๋˜๋Š” ๋ฉ”์„œ๋“œ

axios.get("url"[,config])

// axios๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์„ค์น˜ํ•œ axios๋ฅผ ๋ถˆ๋Ÿฌ์™€์•ผ ํ•ฉ๋‹ˆ๋‹ค.
import axios from 'axios';

// Promise ver
axios
  .get('https://koreanjson.com/users/1')
  .then((response) => {
    const { data } = response;
    console.log(data);
  })
  .catch((error) => console.log(error));

// Async / Await ver
async function request() {
  const response = await axios.get('https://koreanjson.com/users/1');
  const { data } = response;
  console.log(data);
}

request();

๐Ÿ“ก POST ์š”์ฒญ ์˜ˆ์‹œ

POST : ์„œ๋ฒ„์—๊ฒŒ ๋ฐ์ดํ„ฐ๋ฅผ ๋ณด๋‚ด๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ๋˜๋Š” ๋ฉ”์„œ๋“œ

axios.post("url"[, data[, config]])

// axios๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์„ค์น˜ํ•œ axios๋ฅผ ๋ถˆ๋Ÿฌ์™€์•ผ ํ•ฉ๋‹ˆ๋‹ค.
import axios from 'axios';

// Promise ver
axios
  .post('https://koreanjson.com/users', { nickName: 'ApeachIcetea', age: '20' })
  .then((response) => {
    const { data } = response;
    console.log(data);
  })
  .catch((error) => console.log(error));

// Async / Await ver
async function request() {
  const response = await axios.post('https://koreanjson.com/users', {
    name: 'ApeachIcetea',
    age: '20',
  });
  const { data } = response;
  console.log(data);
}

request();

๐Ÿ”ฅ fetch API vs axios

Fetch API
๋นŒํŠธ์ธ API๋ผ ๋ณ„๋„์˜ ์„ค์น˜๊ฐ€ ํ•„์š”์—†๋‹ค.
.json() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.

Axios
์จ๋“œํŒŒํ‹ฐ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋กœ ์„ค์น˜๊ฐ€ ํ•„์š”ํ•˜๋‹ค.
์ž๋™์œผ๋กœ JSON๋ฐ์ดํ„ฐ ํ˜•์‹์œผ๋กœ ๋ณ€ํ™˜๋œ๋‹ค.


๐Ÿ”ฅ POST vs PATCH vs PUT vs GET vs DELETE

๋ฉ”์†Œ๋“œ์—ญํ• ๋ฉฑ๋“ฑ์„ฑ
POSTCreateNO
PATCH์ˆ˜์ • (์š”์ฒญํ•œ ๋ถ€๋ถ„๋งŒ ๋ฐ˜์˜๋˜๋ฉฐ ๋‚˜๋จธ์ง€๋Š” ๊ธฐ์กด์˜ ๋ฐ์ดํ„ฐ๊ฐ€ ์œ ์ง€)NO
PUTUpdate (๋ณ€๊ฒฝ๋˜์ง€ ์•Š๋Š” ๋ฐ์ดํ„ฐ๋„ ๋ชจ๋‘ ์ „๋‹ฌํ•ด์•ผ ํ•จ)YES
GETReadYES
DELETEDeleteYES

โ—๏ธ์ฐธ๊ณ ) ๋ฉฑ๋“ฑ์„ฑ์ด๋ž€ ์—ฌ๋Ÿฌ๋ฒˆ ์ˆ˜ํ–‰ํ•ด๋„ ๊ฒฐ๊ณผ๊ฐ€ ๊ฐ™์Œ์„ ์˜๋ฏธํ•œ๋‹ค.

โœš ํ—ท๊ฐˆ๋ฆฌ๋Š” PATCH์™€ PUT์˜ ๋ฉฑ๋“ฑ์„ฑ
PUT์€ ๋ฐ์ดํ„ฐ๋ฅผ ์™„์ „ํžˆ ๊ต์ฒดํ•ด ๋ฒ„๋ฆฌ๊ธฐ ๋•Œ๋ฌธ์— ๋ฉฑ๋“ฑ์ด๊ณ ,
PATCH๋Š” ๋ฉฑ๋“ฑ์ผ์ˆ˜๋„, ๋ฉฑ๋“ฑ์ด ์•„๋‹์ˆ˜๋„ ์žˆ๋‹ค.

(์˜ˆ๋ฅผ ๋“ค์–ด PATCH๋กœ ๋‹จ์ˆœํžˆ ์œ ์ €์˜ ์ด๋ฆ„์„ ๋ฐ”๊พธ๋Š” ๊ฒƒ์€ ๋ฉฑ๋“ฑํ•˜์ง€๋งŒ,
ํ˜ธ์ถœํ•  ๋•Œ๋งˆ๋‹ค +1์ด ๋œ๋‹ค๋ฉด ๋ฉฑ๋“ฑํ•˜์ง€ ์•Š์€ ๊ฒƒ์ด ๋œ๋‹ค.)

profile
๐Ÿพ

0๊ฐœ์˜ ๋Œ“๊ธ€