Axios๋ž€?

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

๋ฐฑ์—”๋“œ์™€ ํ”„๋ก ํŠธ์—”๋“œ๋ž‘ ํ†ต์‹ ์„ ์‰ฝ๊ฒŒ ํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ๋˜๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ


axios ํŠน์ง•

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

HTTP(HyperText Transfer Protocol)๋ž€? HTML๊ณผ ๊ฐ™์€ ํ•˜์ดํผ๋ฏธ๋””์–ด ๋ฌธ์„œ๋ฅผ ์ „์†กํ•˜๊ธฐ์œ„ํ•œ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋ ˆ์ด์–ด ํ”„๋กœํ† ์ฝœ. ํ†ต์‹  ๊ทœ์•ฝ.



axios ์‚ฌ์šฉ๋ฒ•

axios์˜ Request method๋Š”

  • GET : axios.get(url[, config])
  • POST : axios.post(url, data[, config])
  • PUT : axios.put(url, data[, config])
  • DELETE : axios.delete(url[, config])

axios์—์„œ Request Method๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” axios์— .์„ ๋ถ™ํžˆ๋ฉฐ ์†Œ๋ฌธ์ž๋กœ Req Method๋ฅผ ๋„ฃ์–ด์ฃผ๋ฉด ๋œ๋‹ค.

๊ทธ๋ฆฌ๊ณ  ํ•ด๋‹น ๋ฉ”์„œ๋“œ์˜ ํŒŒ๋ผ๋ฏธํ„ฐ์—๋Š” API์˜ ์ฃผ์†Œ๋ฅผ ๋„ฃ๋Š”๋‹ค.



:: axios์˜ ๊ธฐ๋ณธ์ ์ธ ์‚ฌ์šฉ๋ฒ•

์ผ๋ฐ˜์ ์œผ๋กœ axios์˜ 4๊ฐ€์ง€ ๊ธฐ๋ณธ ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด ์ง€์ •ํ•ด์•ผํ•  ๊ฒƒ๋“ค์ด ์žˆ๋‹ค.

4๊ฐ€์ง€ ๊ธฐ๋ณธ Params

  • Method
  • Url
  • Data (optional)
  • Params (optional)

์ด 4๊ฐ€์ง€ ๋ฐฉ๋ฒ•์„ axios์— ์•Œ๋ ค์ค˜์•ผ ํ•œ๋‹ค.

axios({
  method: "get",
  url: "url",
  responseType: "type",
}).then(function (response) {
  // response Action
});


๋งŒ์•ฝ POST ๋ฉ”์„œ๋“œ์—์„œ data๋ฅผ ์ „์†กํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” url ๋ฐ‘์— data Object๋ฅผ ์ถ”๊ฐ€ํ•˜๋ฉด ๋œ๋‹ค.



:: axios์˜ ๋‹จ์ถ• ์‚ฌ์šฉ๋ฒ•

4๊ฐ€์ง€ ๊ธฐ๋ณธ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์ƒ๋žตํ•˜๊ฑฐ๋‚˜ ๊ฐ„๊ฒฐํ•˜๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค


axios.get()

axios.get()์„ ์‚ฌ์šฉํ•˜๋Š” 2๊ฐ€์ง€ ์ƒํ™ฉ์ด ์žˆ๋Š”๋ฐ 2๊ฐ€์ง€ ์ƒํ™ฉ์— ๋”ฐ๋ผ params: {} ๊ฐ์ฒด๊ฐ€ ์กด์žฌํ• ์ง€ ์•ˆํ• ์ง€๊ฐ€ ๊ฒฐ์ •๋œ๋‹ค.

  • ๋‹จ์ˆœ ๋ฐ์ดํ„ฐ(ํŽ˜์ด์ง€ ์š”์ฒญ, ์ง€์ •๋œ ์š”์ฒญ) ์š”์ฒญ์„ ์ˆ˜ํ–‰ํ•  ๊ฒฝ์šฐ
// callback ์„ ์‚ฌ์šฉํ•  ๋•Œ,
axios
  .get("url")
  .then((response) => {
    // response
  })
  .catch((error) => {
    // ์˜ค๋ฅ˜๋ฐœ์ƒ์‹œ ์‹คํ–‰
  })
  .then(() => {
    // ํ•ญ์ƒ ์‹คํ–‰
  });

// async await ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ,
try {
  const data = await axios.get("url");
} catch {
  // ์˜ค๋ฅ˜ ๋ฐœ์ƒ์‹œ ์‹คํ–‰
}


  • ํŒŒ๋ผ๋ฏธํ„ฐ ๋ฐ์ดํ„ฐ๋ฅผ ํฌํ•จ์‹œํ‚ค๋Š” ๊ฒฝ์šฐ (์‚ฌ์šฉ์ž ๋ฒˆํ˜ธ์— ๋”ฐ๋ฅธ ์กฐํšŒ)
axios.get("url", {
        params: {
          	id: 123
        }
    })
    .then(response => {
         // response
    }).catch(error => {
        // ์˜ค๋ฅ˜๋ฐœ์ƒ์‹œ ์‹คํ–‰
    }).then(() => {
        // ํ•ญ์ƒ ์‹คํ–‰
    });


// async await ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ,
try {
	const data = await axios.get("url",
         params: {
			id: 123
		 }
    );
} catch {
	// ์˜ค๋ฅ˜ ๋ฐœ์ƒ์‹œ ์‹คํ–‰
}


axios.post()

post ๋ฉ”์„œ๋“œ์—๋Š” ์ผ๋ฐ˜์ ์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ Message Body์— ํฌํ•จ์‹œ์ผœ ๋ณด๋‚ธ๋‹ค. get ๋ฉ”์„œ๋“œ์—์„œ params๋ฅผ ์‚ฌ์šฉํ•œ ๊ฒฝ์šฐ์™€ ๋น„์Šทํ•˜๊ฒŒ ์ˆ˜ํ–‰๋œ๋‹ค.

axios
  .post("url", {
    username: "",
    password: "",
  })
  .then((response) => {
    // response
  })
  .catch((error) => {
    // ์˜ค๋ฅ˜๋ฐœ์ƒ์‹œ ์‹คํ–‰
  })
  .then(() => {
    // ํ•ญ์ƒ ์‹คํ–‰
  });

// async await ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ,
try {
  const data = await axios.post("url");
} catch {
  // ์˜ค๋ฅ˜ ๋ฐœ์ƒ์‹œ ์‹คํ–‰
}


axios.put()

put ๋ฉ”์„œ๋“œ๋Š” ์„œ๋ฒ„ ๋‚ด๋ถ€์ ์œผ๋กœ get โž” post ๊ณผ์ •์„ ๊ฑฐ์น˜๊ธฐ ๋•Œ๋ฌธ์— post ๋ฉ”์„œ๋“œ์™€ ๋น„์Šทํ•œ ํ˜•ํƒœ์ด๋‹ค.

axios
  .put("url", {
    username: "",
    password: "",
  })
  .then((response) => {
    // response
  })
  .catch((error) => {
    // ์˜ค๋ฅ˜๋ฐœ์ƒ์‹œ ์‹คํ–‰
  })
  .then(() => {
    // ํ•ญ์ƒ ์‹คํ–‰
  });

// async await ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ,
try {
  const data = await axios.put("url", {
    username: "",
    password: "",
  });
} catch {
  // ์˜ค๋ฅ˜ ๋ฐœ์ƒ์‹œ ์‹คํ–‰
}


axios.delete()

delete ๋ฉ”์„œ๋“œ์—๋Š” ์ผ๋ฐ˜์ ์œผ๋กœ body๊ฐ€ ๋น„์–ด์žˆ๋‹ค. ๊ทธ๋ž˜์„œ ํ˜•ํƒœ๋Š” get๊ณผ ๋น„์Šทํ•œ ํ˜•ํƒœ๋ฅผ ๋„์ง€๋งŒ ํ•œ ๋ฒˆ delete ๋ฉ”์„œ๋“œ๊ฐ€ ์„œ๋ฒ„์— ๋“ค์–ด๊ฐ€๊ฒŒ ๋œ๋‹ค๋ฉด ์„œ๋ฒ„ ๋‚ด์—์„œ ์‚ญ์ œ process๋ฅผ ์ง„ํ–‰ํ•˜๊ฒŒ ๋œ๋‹ค.

axios
  .delete("/user?ID=12345")
  .then((response) => {
    // handle success
    console.log(response);
  })
  .catch((error) => {
    // handle error
    console.log(error);
  })
  .then(() => {
    // always executed
  });

// async await ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ,
try {
  const data = await axios.delete("url");
} catch {
  // ์˜ค๋ฅ˜ ๋ฐœ์ƒ์‹œ ์‹คํ–‰
}


๋งŽ์€ ๋ฐ์ดํ„ฐ๋ฅผ ์š”์ฒญํ•  ๊ฒฝ์šฐ ํ•˜์ง€๋งŒ query๋‚˜ params๊ฐ€ ๋งŽ์•„์ ธ์„œ ํ—ค๋”์— ๋งŽ์€ ์ •๋ณด๋ฅผ ๋‹ด์„ ์ˆ˜ ์—†์„ ๋•Œ๋Š” ๋‘ ๋ฒˆ์งธ ์ธ์ž์— data๋ฅผ ์ถ”๊ฐ€ํ•ด์ค„ ์ˆ˜ ์žˆ๋‹ค.

axios
  .delete("/user?ID=12345", {
    data: {
      post_id: 1,
      comment_id: 5,
      username: "april",
    },
  })
  .then((response) => {
    // handle success
    console.log(response);
  })
  .catch((error) => {
    // handle error
    console.log(error);
  })
  .then(() => {
    // always executed
  });

// async await ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ,
try {
  const data = await axios.delete("url");
} catch {
  // ์˜ค๋ฅ˜ ๋ฐœ์ƒ์‹œ ์‹คํ–‰
}



:: React์—์„œ axios ์‚ฌ์šฉ

๋ณดํ†ต ํ”„๋กœ์ ํŠธ์—์„œ๋Š”

  • API์˜ ์ŠคํŽ™, ์„œ๋ฒ„์˜ ์ฃผ์†Œ, credentials ์„ค์ •์„ ๋‹ด๋‹นํ•˜๋Š” API.js ๋ผ๋Š” ํŒŒ์ผ์„ ๋งŒ๋“ค๊ณ 
  • axios ์˜ ๊ธฐ๋ณธ ์„ค์ •์— ๋Œ€ํ•ด์„œ ์ง€์ •ํ•ด์ฃผ๊ณ 
  • ๊ฐ๊ฐ์˜ ์„œ๋น„์Šค์—์„œ ๊ฐ€์ ธ๊ฐ€ ์‚ฌ์šฉํ•˜๋Š” ํ˜•ํƒœ๋กœ ๊ตฌํ˜„.

axios ์˜ ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ/async await ํ†ต์‹ 

๋จผ์ € axios ์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•ด์„œ API๋ผ๋Š” ๋ณ€์ˆ˜์— ๋‹ด๊ณ  API๋ฅผ ๋ฐ˜ํ™˜์‹œํ‚จ๋‹ค.

// API.js
// axios ์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑ
import axios from "axios";

const API = axios.create({
  BASE_URL: "",
  headers: {
    "Content-Type": "application/json",
  },
  withCredentials: true,
});

export default API;


๊ฐ๊ฐ ํŒŒ์ผ์—์„œ๋Š” async await ์œผ๋กœ ์ฝœ๋ฐฑ์„ ์ฒ˜๋ฆฌํ•˜์—ฌ ํ†ต์‹ ํ•œ๋‹ค.

import API from "../utils/API";

export const login = async (code) => {
  const { data } = await API.post("url", JSON.stringify(code));
  return data;
};


Error Handling์„ ์œ„ํ•œ try-catch๋ฌธ ์‚ฌ์šฉ

import API from "../utils/API";

export const refresh = async () => {
  try {
    const { data } = await API.get("url");
    return data;
  } catch {
    // Error Handling
  }
};


React axios