Axios

ํ•™์Šต ํ‚ค์›Œ๋“œ

  • Axios

    • Ajax

    • Fetch vs Axios

  • ์‚ฌ์šฉ๋ฒ•

  • Node.js์™€ ๋ธŒ๋ผ์šฐ์ €๋ฅผ ์œ„ํ•œ Promise ๊ธฐ๋ฐ˜ HTTP ํด๋ผ์ด์–ธํŠธ (HTTP ๋น„๋™๊ธฐ ํ†ต์‹  ํด๋ผ์ด์–ธํŠธ)

ํŠน์ง•

  • ๋ธŒ๋ผ์šฐ์ €๋ฅผ ์œ„ํ•ด XMLHttpRequests ์ƒ์„ฑ

  • node.js๋ฅผ ์œ„ํ•ด http ์š”์ฒญ ์ƒ์„ฑ

  • Promise API๋ฅผ ์ง€์›

  • ์š”์ฒญ ๋ฐ ์‘๋‹ต ์ธํ„ฐ์…‰ํŠธ

  • ์š”์ฒญ ๋ฐ ์‘๋‹ต ๋ฐ์ดํ„ฐ ๋ณ€ํ™˜

  • ์š”์ฒญ ์ทจ์†Œ

  • โœ… JSON ๋ฐ์ดํ„ฐ ์ž๋™ ๋ณ€ํ™˜

  • XSRF๋ฅผ ๋ง‰๊ธฐ์œ„ํ•œ ํด๋ผ์ด์–ธํŠธ ์‚ฌ์ด๋“œ ์ง€์›

๐Ÿค” ์™œ Axios๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์ด์œ ?

์šฐ์„  React๋Š” ๋น„๋™๊ธฐ ํ†ต์‹ ์„ ์œ„ํ•œ HTTP Client๋ฅผ ๊ตฌํ˜„ํ•˜๊ธฐ ์œ„ํ•œ ๊ธฐ๋Šฅ์ด ์ œ๊ณต ๋˜์ง€ ์•Š๋Š”๋‹ค. ๋”ฐ๋ผ์„œ React Ajax๋ฅผ ๊ตฌํ˜„ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” JavaScript ๋‚ด์žฅ ๊ฐ์ฒด์ธ XMLRequest๋ฅผ ์‚ฌ์šฉํ•˜๊ฑฐ๋‚˜ , ๋‹ค๋ฅธ HTTP Client๋ฅผ ์‚ฌ์šฉ ํ•ด์•ผ ํ•œ๋‹ค.

๋ธŒ๋ผ์šฐ์ €๊ฐ€ ์ œ๊ณตํ•˜๋Š” Fetch API๋ฅผ ์‚ฌ์šฉํ•ด๋„ ๋˜์ง€๋งŒ ๋ณด๋‹ค ๋” ๋งŽ์€ ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•ด์„œ ์‚ฌ๋žŒ๋“ค์˜ ์„ ํ˜ธ๋„๊ฐ€ ๋†’์•„ ๋งŽ์ด ์‚ฌ์šฉ๋œ๋‹ค.

๐Ÿ“– Ajax

  • ๋ธŒ๋ผ์šฐ์ €๊ฐ€ ๊ฐ–๊ณ  ์žˆ๋Š” XMLHttpRequest ๊ฐ์ฒด๋ฅผ ์ด์šฉํ•ด์„œ ์ „์ฒด ํŽ˜์ด์ง€๋ฅผ ์ƒˆ๋กœ๊ณ ์นจ ํ•˜์ง€ ์•Š์•„๋„ ํŽ˜์ด์ง€์˜ ์ผ๋ถ€๋ถ„๋งŒ์„ ์œ„ํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ๋กœ๋“œํ•˜๋Š” ๊ธฐ๋ฒ•

  • JavaScript๋ฅผ ์‚ฌ์šฉํ•œ ๋น„๋™๊ธฐ ํ†ต์‹ , ํด๋ผ์ด์–ธํŠธ์™€ ์„œ๋ฒ„ ๊ฐ„์— XML ๋ฐ์ดํ„ฐ๋ฅผ ์ฃผ๊ณ  ๋ฐ›๋Š” ๊ธฐ์ˆ 

Fetch vs Axios

FetchAxios

์œ ํ˜•

๋ธŒ๋ผ์šฐ์ € ๋นŒํŠธ์ธ

์จ๋“œํŒŒํ‹ฐ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ

๋ฐ์ดํ„ฐ

data ์†์„ฑ์„ ์‚ฌ์šฉ

body ์†์„ฑ์„ ์‚ฌ์šฉ

๋ฐ์ดํ„ฐ ํƒ€์ž…

data๋Š” object๋ฅผ ํฌํ•จํ•œ๋‹ค.

body๋Š” ๋ฌธ์ž์—ดํ™” ๋˜์–ด์žˆ๋‹ค.

์‘๋‹ต

status๊ฐ€ 200์ด๊ณ  statusText๊ฐ€ โ€˜OKโ€™์ด๋ฉด ์„ฑ๊ณต์ด๋‹ค.

์‘๋‹ต๊ฐ์ฒด๊ฐ€ ok ์†์„ฑ์„ ํฌํ•จํ•˜๋ฉด ์„ฑ๊ณต์ด๋‹ค.

json

์ž๋™์œผ๋กœ JSON๋ฐ์ดํ„ฐ ํ˜•์‹์œผ๋กœ ๋ณ€ํ™˜

.json()๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉ

์š”์ฒญ ์ปค์Šคํ…€

์š”์ฒญ์„ ์ทจ์†Œํ•  ์ˆ˜ ์žˆ๊ณ  ํƒ€์ž„์•„์›ƒ์„ ๊ฑธ ์ˆ˜ ์žˆ๋‹ค.

ํ•ด๋‹น ๊ธฐ๋Šฅ ์กด์žฌ ํ•˜์ง€์•Š์Œ

HTTP ์š”์ฒญ

HTTP ์š”์ฒญ์„ ๊ฐ€๋กœ์ฑŒ์ˆ˜ ์žˆ์Œ

๊ธฐ๋ณธ์ ์œผ๋กœ ์ œ๊ณตํ•˜์ง€ ์•Š์Œ

download

download ์ง„ํ–‰์— ๋Œ€ํ•ด ๊ธฐ๋ณธ์ ์ธ ์ง€์›์„ ํ•จ

์ง€์›ํ•˜์ง€ ์•Š์Œ

์ง€์› ๋ธŒ๋ผ์šฐ์ €

์ข€ ๋” ๋งŽ์€ ๋ธŒ๋ผ์šฐ์ €์— ์ง€์›๋จ

Chrome 42+, Firefox 39+, Edge 14+, and Safari 10.1+

โš™๏ธ ์„ค์น˜ ๋ฐ ์‚ฌ์šฉ๋ฒ•

npm install axios

get ์š”์ฒญ

// Promise chaining
axios
  .get('/user', {params : { ID : 12345 },
  })
  .then((response) => console.log(response))
  .catch((error) => console.log(error) )
  .then(() => console.log('done'));
// async await
async function getUser() {
  try {
    const response = await axios.get('/user?ID=12345');
    return response.data;
  } catch (error) {
    console.log(error);
  }
}

post ์š”์ฒญ

// Promise chaining
axios.post('/user', {
    firstName: 'Fred',
    lastName: 'Flintstone'
  })
  .then(function (response) {
    console.log(response);
  })
  .catch(function (error) {
    console.log(error);
  });
// async await
async function postUser() {
  try {
    const response = await axios.post('/user',  {
    firstName: 'Fred',
    lastName: 'Flintstone'
  });
  return response.data;

  } catch (error) {
    console.log(error);
  }
}

๐Ÿ”— ์ฐธ๊ณ 

Last updated