Jieunny์˜ ๋ธ”๋กœ๊ทธ

S2) Unit 3. [JS] fetch & Axios ๋ณธ๋ฌธ

CodeStates/learning contents

S2) Unit 3. [JS] fetch & Axios

Jieunny 2023. 1. 19. 13:00

๐Ÿ“ฃ fetch API

โœ”๏ธ ๋™์ ์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›์•„์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ ์‚ฌ์šฉํ•œ๋‹ค.

โœ”๏ธ ํŠน์ • URL๋กœ๋ถ€ํ„ฐ ์ •๋ณด๋ฅผ ๋ฐ›์•„์˜ค๋Š” ์—ญํ• ์„ ํ•œ๋‹ค.

โœ”๏ธ ๊ณผ์ •์ด ๋น„๋™๊ธฐ๋กœ ์ด๋ฃจ์–ด์ง€๊ธฐ ๋•Œ๋ฌธ์— ์‹œ๊ฐ„์ด ์†Œ์š”๋˜๋Š” ์ž‘์—…์„ ์š”๊ตฌํ•  ๊ฒฝ์šฐ์—๋Š” blocking์ด ๋ฐœ์ƒํ•˜๋ฉด ์•ˆ๋˜๋ฏ€๋กœ ํŠน์ • DOM์— ์ •๋ณด๊ฐ€ ํ‘œ์‹œ๋  ๋–„๊นŒ์ง€ ๋กœ๋”ฉ ์ฐฝ์„ ๋Œ€์‹  ๋„์šฐ๋Š” ๊ฒฝ์šฐ๋„ ์žˆ๋‹ค.

let url =
  "https://koreanjson.com/posts/1";
fetch(url)
  .then((response) => response.json())
  .then((json) => console.log(json))
  .catch((error) => console.log(error));
  
  
  //{id: 1, title: '์ •๋‹น์˜ ๋ชฉ์ ์ด๋‚˜ ํ™œ๋™์ด ๋ฏผ์ฃผ์  ๊ธฐ๋ณธ์งˆ์„œ์— ์œ„๋ฐฐ๋  ๋•Œ์—๋Š” ์ •๋ถ€๋Š” ํ—Œ๋ฒ•์žฌํŒ์†Œ์— ๊ทธ ํ•ด์‚ฐ์„ ์ œ์†Œํ•  ์ˆ˜ ์žˆ๊ณ , 
  //์ •๋‹น์€ ํ—Œ๋ฒ•์žฌํŒ์†Œ์˜ ์‹ฌํŒ์— ์˜ํ•˜์—ฌ ํ•ด์‚ฐ๋œ๋‹ค.', content: '๋ชจ๋“  ๊ตญ๋ฏผ์€ ์ธ๊ฐ„์œผ๋กœ์„œ์˜ ์กด์—„๊ณผ ๊ฐ€์น˜๋ฅผ ๊ฐ€์ง€๋ฉฐ, 
  //ํ–‰๋ณต์„ ์ถ”๊ตฌํ•  ๊ถŒ๋ฆฌ๋ฅผ ๊ฐ€์ง„๋‹ค. ๋ชจ๋“  ๊ตญ๋ฏผ์€…์ง„๋‹ค. ๋ˆ„๊ตฌ๋“ ์ง€ ์ฒดํฌ ๋˜๋Š” ๊ตฌ์†์„ ๋‹นํ•œ ๋•Œ์—๋Š” ์ฆ‰์‹œ ๋ณ€ํ˜ธ์ธ์˜ ์กฐ๋ ฅ์„ ๋ฐ›์„ ๊ถŒ๋ฆฌ๋ฅผ ๊ฐ€์ง„๋‹ค.', 
  //createdAt: '2019-02-24T16:17:47.000Z', updatedAt: '2019-02-24T16:17:47.000Z', …}

โžฐ ๊ฐœ๋ฐœ์ž ๋„๊ตฌ์˜ ์ฝ˜์†”์—์„œ fetch API๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฅผ ์š”์ฒญํ•œ๋‹ค.

โžฐ ์œ„ ์ฒ˜๋Ÿผ ์ฃผ์–ด์ง„ url์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›์•„์˜จ๋‹ค.

 

๐Ÿ“ฃ Axios

โœ”๏ธ ๋ธŒ๋ผ์šฐ์ €, Node.js๋ฅผ ์œ„ํ•œ Promise API๋ฅผ ํ™œ์šฉํ•˜๋Š” HTTP ๋น„๋™๊ธฐ ํ†ต์‹  ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์ด๋‹ค.

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

 

โœ”๏ธ Axios ์‚ฌ์šฉ๋ฒ•

 

1๏ธโƒฃ ์„ค์น˜

npm install axios

 

2๏ธโƒฃ GET ์š”์ฒญ

โœ”๏ธ ์ผ๋ฐ˜์ ์œผ๋กœ ์ •๋ณด๋ฅผ ์š”์ฒญํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ๋˜๋Š” ๋ฉ”์„œ๋“œ

โœ”๏ธ ์ฒซ ๋ฒˆ์งธ ์ธ์ž์—๋Š” url์ฃผ์†Œ๊ฐ€ ํ•„์ˆ˜์ ์œผ๋กœ ๋“ค์–ด๊ฐ€๊ณ , ๋‘ ๋ฒˆ์งธ ์ธ์ž์—๋Š” ์š”์ฒญ ์‹œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์˜ต์…˜๋“ค์„ ์„ ํƒ์ ์œผ๋กœ ๋„ฃ์–ด์ค„ ์ˆ˜ ์žˆ๋‹ค.

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

 

3๏ธโƒฃ POST ์š”์ฒญ

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

โœ”๏ธ ์ฒซ ๋ฒˆ์งธ ์ธ์ž์—๋Š” url์ฃผ์†Œ๊ฐ€ ํ•„์ˆ˜์ ์œผ๋กœ ๋“ค์–ด๊ฐ€๊ณ , ๋‘ ๋ฒˆ์งธ ์ธ์ž์—๋Š” ์š”์ฒญ ์‹œ ๋ณด๋‚ผ ๋ฐ์ดํ„ฐ๋ฅผ ์ƒํ™ฉ์— ๋”ฐ๋ผ ์„ค์ •ํ•ด์ค€๋‹ค.

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

 

 

๐Ÿ“ฃ Fetch์™€ Axios์˜ ์‚ฌ์šฉ ์˜ˆ์‹œ

1๏ธโƒฃ GET ์š”์ฒญ

// fetch

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

 request();
// axios

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

 

 

2๏ธโƒฃ POST ์š”์ฒญ

// fetch

// 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๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์„ค์น˜ํ•œ 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();

'CodeStates > learning contents' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

S2) Unit 5. [React] React SPA  (0) 2023.01.25
S2) Unit 4. [React] ๊ธฐ์ดˆ  (0) 2023.01.20
S2) Unit 3. [JS] Node.js  (0) 2023.01.18
S2) Unit 3. [JS] ๋น„๋™๊ธฐ(Callback, Promise, Async/Await)  (0) 2023.01.17
S2) Unit 2. [JS] ํ”„๋กœํ† ํƒ€์ž…  (0) 2023.01.13