๊ด€๋ฆฌ ๋ฉ”๋‰ด

Daily Front_Minhhk

[JS] Fetch API // Axios ๋ณธ๋ฌธ

์นดํ…Œ๊ณ ๋ฆฌ ์—†์Œ

[JS] Fetch API // Axios

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

 

Fetch API vs Axios

 

Axios Fetch API
์จ๋“œํŒŒํ‹ฐ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋กœ ์„ค์น˜๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ๋นŒํŠธ์ธ API๋ผ ๋ณ„๋„์˜ ์„ค์น˜ ํ•„์š”์—†์Šต๋‹ˆ๋‹ค.
์ž๋™์œผ๋กœ JSON๋ฐ์ดํ„ฐ ํ˜•์‹์œผ๋กœ ๋ณ€ํ™˜๋ฉ๋‹ˆ๋‹ค. .json() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

 

Axios๋Š” ์จ๋“œํŒŒํ‹ฐ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์ด๊ธฐ ๋•Œ๋ฌธ์— ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์„ค์น˜

 โžก๏ธ npm install axios

 


GET ์š”์ฒญ

GET ์š”์ฒญ์€ ์ผ๋ฐ˜์ ์œผ๋กœ ์ •๋ณด๋ฅผ ์š”์ฒญํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ๋˜๋Š” ๋ฉ”์„œ๋“œ์ž…๋‹ˆ๋‹ค.

์ฒซ๋ฒˆ์งธ ์ธ์ž์—๋Š” url์ฃผ์†Œ๊ฐ€ ๋“ค์–ด๊ฐ‘๋‹ˆ๋‹ค.

url์ฃผ์†Œ๋Š” ํ•„์ˆ˜์ž…๋‹ˆ๋‹ค.

๋‘๋ฒˆ์งธ ์ธ์ž์—๋Š” ์š”์ฒญ ์‹œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์˜ต์…˜๋“ค์„ ์„ค์ •ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

์˜ต์…˜์˜ ๊ฒฝ์šฐ ํ•„์ˆ˜๋Š” ์•„๋‹™๋‹ˆ๋‹ค.

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

fetch API

Promise

// 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

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

request();

.json()์€ Response ์ŠคํŠธ๋ฆผ์„ ๊ฐ€์ ธ์™€ ์ŠคํŠธ๋ฆผ์ด ์™„๋ฃŒ๋ ๋•Œ๊นŒ์ง€ ์ฝ๋Š”๋‹ค.

๊ทธ๋ฆฌ๊ณ  ๋‹ค ์ฝ์€ body์˜ ํ…์ŠคํŠธ๋ฅผ Promiseํ˜•ํƒœ๋กœ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

axios

Promise

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

// Promise ver
axios
  .get('<https://koreanjson.com/users/1>')
  .then((response) => {
    const { data } = response; // response ์— ๋ฐ›์•„์™€์„œ data ํ• ๋‹น
    console.log(data); // object
  })
  .catch((error) => console.log(error));

async_await

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

// 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 ์š”์ฒญ์€ ์„œ๋ฒ„์—๊ฒŒ ๋ฐ์ดํ„ฐ๋ฅผ ๋ณด๋‚ด๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ๋˜๋Š” ๋ฉ”์„œ๋“œ์ž…๋‹ˆ๋‹ค.

์ฒซ๋ฒˆ์งธ ์ธ์ž์—๋Š” url์ฃผ์†Œ๊ฐ€ ๋“ค์–ด๊ฐ‘๋‹ˆ๋‹ค.

url์ฃผ์†Œ๋Š” ํ•„์ˆ˜์ž…๋‹ˆ๋‹ค.

๋‘๋ฒˆ์งธ ์ธ์ž์—๋Š” ์š”์ฒญ ์‹œ ๋ณด๋‚ผ ๋ฐ์ดํ„ฐ๋ฅผ ์„ค์ •ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

์˜ต์…˜์˜ ๊ฒฝ์šฐ ํ•„์ˆ˜๋Š” ์•„๋‹ˆ์ง€๋งŒ ์ƒํ™ฉ์— ๋”ฐ๋ผ ์„ค์ •ํ•ด์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

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

fetch API

Promise

// 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

// 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

Promise

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

// Promise ver
axios
  .post('<https://koreanjson.com/users>', { nickName: 'ApeachIcetea', age: '20' })
  .then((response) => {
    // console.log(response) 
    const { data } = response; // ์ค‘๊ด„ํ˜ธ๋กœ ๋ฐ›์•„์˜ค์ž ์ž…๋ ฅํ•œ ๊ฐ์ฒด ์ •๋ณด data ๋ฅผ
    console.log(data); // { nickName: 'ApeachIcetea', age: '20' }
  })
  .catch((error) => console.log(error));

async_await

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

// 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();