์ƒˆ์†Œ์‹

๐Ÿ“ŒDevelop/โš›๏ธReact

[React] axios vs fetch / axios fetch ์ฐจ์ด / axios fetch ์ฐจ์ด์ 

  • -

 

 

 

 

 

 

axiso

Axios ๋Š” ๋ธŒ๋ผ์šฐ์ €, Node.js ๋ฅผ ์œ„ํ•œ Promise API ๋ฅผ ํ™œ์šฉํ•˜๋Š” HTTP ๋น„๋™๊ธฐ ํ†ต์‹  ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ(return ์„ promise ๊ฐ์ฒด๋กœ ํ•ด์ฃผ๊ธฐ ๋•Œ๋ฌธ์—, response ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค๋ฃจ๊ธฐ ์‰ฌ์šด ์žฅ์ )

axios ํŠน์ง•

  • ์šด์˜ ํ™˜๊ฒฝ์— ๋”ฐ๋ผ ๋ธŒ๋ผ์šฐ์ €์˜ XMLHttpRequest ๊ฐ์ฒด ๋˜๋Š” Node.js์˜ http api ์‚ฌ์šฉ
  • Promise(ES6) API ์‚ฌ์šฉ
  • ์š”์ฒญ๊ณผ ์‘๋‹ต ๋ฐ์ดํ„ฐ์˜ ๋ณ€ํ˜•
  • HTTP ์š”์ฒญ ์ทจ์†Œ
  • HTTP ์š”์ฒญ๊ณผ ์‘๋‹ต์„ JSON ํ˜•ํƒœ๋กœ ์ž๋™ ๋ณ€๊ฒฝ

axios ๊ธฐ์ดˆ๋ฌธ๋ฒ•

/* axios ํŒŒ๋ผ๋ฏธํ„ฐ ๋ฌธ๋ฒ• ์˜ˆ์‹œ */

axios({
    method: "get", // ํ†ต์‹  ๋ฐฉ์‹(GET/POST)
    url: "www.google.com", // ์„œ๋ฒ„
    headers: {'X-Requested-With': 'XMLHttpRequest'} // ์š”์ฒญ ํ—ค๋” ์„ค์ •
    params: { name: "ooweat", blog: "tistory" }, // ?ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์ „๋‹ฌ
    responseType: 'json', // default
    
    maxContentLength: 2000, // http ์‘๋‹ต ๋‚ด์šฉ์˜ max ์‚ฌ์ด์ฆˆ
    validateStatus: function (status) {
      return status >= 200 && status < 300; // default
    }, // HTTP์‘๋‹ต ์ƒํƒœ ์ฝ”๋“œ์— ๋Œ€ํ•ด promise์˜ ๋ฐ˜ํ™˜ ๊ฐ’์ด resolve ๋˜๋Š” reject ํ• ์ง€ ์ง€์ •
    proxy: {
      host: '127.0.0.1',
      port: 3100,
      auth: {
        username: 'ooweat',
        password: '1234'
      }
    }, // proxy์„œ๋ฒ„์˜ hostname๊ณผ port๋ฅผ ์ •์˜
    maxRedirects: 5, // node.js์—์„œ ์‚ฌ์šฉ๋˜๋Š” ๋ฆฌ๋‹ค์ด๋ ‰ํŠธ ์ตœ๋Œ€์น˜๋ฅผ ์ง€์ •
    httpsAgent: new https.Agent({ keepAlive: true }), // node.js์—์„œ https๋ฅผ ์š”์ฒญ์„ ํ• ๋•Œ ์‚ฌ์šฉ์ž ์ •์˜ agent๋ฅผ ์ •์˜
})
.then(function (response) {
	console.log(response.data)
    // response Action
});

axios ์‘์šฉ๋ฌธ๋ฒ•

const API =axios.create({
    baseURL: `http://localhost:8080/`,    
    timeout: 30000,
    headers: {
        "Content-Type" : `application/json;charset=UTF-8`,
        "Access-Control-Allow-Origin": "*",
        "Accept": "application/json",
    }
});

let [resData, setTotAmt] = useState({
        totAmount : 0,
})

API.post("/common/test", reqData)

 

 

fetch

Javascript ์—์„œ ๊ธฐ๋ณธ์ ์œผ๋กœ ์ œ๊ณตํ•˜๋Š” ๊ธฐ๋Šฅ์œผ๋กœ, Promise ๊ธฐ๋ฐ˜์œผ๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ์–ด axios ๊ฐ€ ๋“ฑ์žฅํ•˜๊ธฐ ์ „, ๋น„๋™๊ธฐ ์ฒ˜๋ฆฌ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๊ฐœ๋ฐœ ์‹œ, ํ•„์ˆ˜์ ์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ๊ธฐ๋Šฅ์ด๋‹ค.

 

fetch ๋Š” ๋˜‘๊ฐ™์€ ์ฝ”๋“œ๊ฐ€ ๋ฐ˜๋ณต๋˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ๊ธฐ ๋•Œ๋ฌธ์—, ๋ชจ๋“ˆํ™”๋ฅผ ํ†ตํ•ด ๋”ฐ๋กœ ์„ ์–ธํ•ด๋†“๊ณ  ๋ณ€์ˆ˜๋ฅผ ํ• ๋‹นํ•˜๋Š” ๊ฒƒ์ด ํŽธ์˜์„ฑ์ด ์šฉ์ดํ•˜๋‹ค.

 

async function post(host, path, body, headers = {}) {
  const url = `https://${host}/${path}`;
  const options = {
    method: "POST",
    headers: {
      "Content-Type": "application/json",
      ...headers,
    },
    body: JSON.stringify(body),
  };
  const res = await fetch(url, options);
  const data = await res.json();
  if (res.ok) {
    return data;
  } else {
    throw Error(data);
  }
}

//์‚ฌ์šฉ ์‹œ, 
post("news.naver.com", "posts", {
  title: "๋‚ ์”จ",
  body: "ํ™”์ฐฝ",
  contentSeq: 1049,
})
  .then((data) => console.log(data))
  .catch((error) => console.log(error));

fetch ๊ธฐ์ดˆ ๋ฌธ๋ฒ•

fetch("https://news.naver.com", {
  method: "POST", // GET(Default), POST, PUT(์ „์ฒด์ˆ˜์ •), PATCH(์ผ๋ถ€์ˆ˜์ •), DELETE(์‚ญ์ œ)
  headers: { // ํ—ค๋” ์กฐ์ž‘
    "Content-Type": "application/json",
  },
  body: JSON.stringify({ // ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๊ฐ์ฒด๋ฅผ jsonํ™” ํ•œ๋‹ค.
    title: "๋‚ ์”จ",
    body: "ํ™”์ฐฝํ•œ ๋ด„๋‚ !",
    contentSeq: 1049,
  }),
})
  .then((response) => response.json())
  .then((data) => console.log(data))

 

fetch ์‘์šฉ ๋ฌธ๋ฒ•

fetch(myRequest)
  .then(response => {
    if (response.status === 200) {
      return response.json();
    } else {
      throw new Error('Something went wrong on api server!');
    }
  })
  .then(response => {
    console.debug(response);
    // ...
  }).catch(error => {
    console.error(error);
  });
#Index.jsx
import React, {useEffect, useState} from "react";
import API from '../util/api.js';
import {useParams} from "react-router-dom";
import Loader from "../component/loader";

useEffect(() => {
        async function callAPI() {
            const res = await API.post("/common/info", reqData)
            console.log(res.data)
            setResData(res.data)
            setLoading(false)
        }

        setLoading(true)
        callAPI().then(r => console.log("Connected"))
    }, []);

 

axios vs fetch ์˜ ์ฐจ์ด์ 

axios fetch
3rd Party lib ๋กœ ์„ค์น˜๊ฐ€ ํ•„์š” ํ˜„๋Œ€ ๋ธŒ๋ผ์šฐ์ €์— ๋นŒํŠธ์ธ์ด๋ผ ์„ค์น˜ ํ•„์š” ์—†์Œ
XSRF ๋ณดํ˜ธ๋ฅผ ํ•ด์ค€๋‹ค. ๋ณ„๋„ ๋ณดํ˜ธ ์—†์Œ
data ์†์„ฑ์„ ์‚ฌ์šฉ body ์†์„ฑ์„ ์‚ฌ์šฉ
data๋Š” object๋ฅผ ํฌํ•จํ•œ๋‹ค body๋Š” ๋ฌธ์ž์—ดํ™”
status๊ฐ€ 200์ด๊ณ  statusText๊ฐ€ ‘OK’์ด๋ฉด ์„ฑ๊ณต์ด๋‹ค ์‘๋‹ต๊ฐ์ฒด๊ฐ€ ok ์†์„ฑ์„ ํฌํ•จํ•˜๋ฉด ์„ฑ๊ณต์ด๋‹ค
JSON๋ฐ์ดํ„ฐ ํ˜•์‹์œผ๋กœ ์ž๋™๋ณ€ํ™˜ .json()๋ฉ”์„œ๋“œ ์„ ์–ธ ํ•„์š”
์š”์ฒญ์„ ์ทจ์†Œํ•  ์ˆ˜ ์žˆ๊ณ  ํƒ€์ž„์•„์›ƒ์ด ๊ฐ€๋Šฅํ•˜๋‹ค. ํ•ด๋‹น ๊ธฐ๋Šฅ ์กด์žฌ ํ•˜์ง€์•Š์Œ
HTTP ์š”์ฒญ์„ ๊ฐ€๋กœ์ฑŒ์ˆ˜ ์žˆ์Œ ๊ธฐ๋ณธ์ ์œผ๋กœ ์ œ๊ณตํ•˜์ง€ ์•Š์Œ
Download ์ง„ํ–‰์— ๋Œ€ํ•ด ๊ธฐ๋ณธ์ ์ธ ์ง€์›์„ ํ•จ ์ง€์›ํ•˜์ง€ ์•Š์Œ
๋Œ€๋ถ€๋ถ„์˜ ๋ธŒ๋ผ์šฐ์ €๋ฅผ ์ง€์›ํ•จ Chrome 42+, Firefox 39+, Edge 14+, and Safari 10.1+์ด์ƒ์— ์ง€์›

 



 

 

 

๋ฐ˜์‘ํ˜•

'๐Ÿ“ŒDevelop > โš›๏ธReact' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[React] date format / moment / react YYYYMMDD YYYY-MM-DD  (0) 2023.02.15
Contents

ํฌ์ŠคํŒ… ์ฃผ์†Œ๋ฅผ ๋ณต์‚ฌํ–ˆ์Šต๋‹ˆ๋‹ค

์ด ๊ธ€์ด ๋„์›€์ด ๋˜์—ˆ๋‹ค๋ฉด ๊ณต๊ฐ ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค.