๐Ÿญ axios ๋ฉ”์†Œ๋“œ ์‚ฌ์šฉ๋ฒ•

KHWยท2021๋…„ 10์›” 18์ผ
1

ํ”„๋ ˆ์ž„์›Œํฌ

๋ชฉ๋ก ๋ณด๊ธฐ
22/43

axios๋ž€?

npm์„ ์ด์šฉํ•˜์—ฌ ๋‹ค์šด๋กœ๋“œ ๊ฐ€๋Šฅํ•œ HTTP request ๋ชจ๋“ˆ ํ˜น์€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ

์•Œ์•„์•ผํ• ๊ฒƒ

postMan์—์„œ๋Š” ์ „๋ถ€"key" : "๊ฐ’"๋กœ ๊ฐ์‹ธ๊ณ 
๋‹ค๋ฅธ๊ณณ์—์„œ๋Š” key : '๊ฐ’' ํ˜•ํƒœ๋กœ ์ฒ˜๋ฆฌ

Get ์‚ฌ์šฉ๋ฒ•

fetch ์‚ฌ์šฉ๋ฒ•

JSON ์‚ฌ์ดํŠธ๋ฅผ ์ด์šฉํ•˜์—ฌ fetch๋ฅผ ์‚ฌ์šฉํ–ˆ๋‹ค.

  • 1๋ฒˆ์งธ todos๋ฅผ ๋ฐ›์•„์™€์„œ ๋ณด์—ฌ์ฃผ๊ณ  ์žˆ๋‹ค.

axios ์‚ฌ์šฉ๋ฒ•

checkGet ํ•จ์ˆ˜๊ฐ€ ์‹คํ–‰๋˜๋ฉด ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜ฌ ์ˆ˜ ์žˆ๊ฒŒ ํ•˜์˜€๋‹ค.

  • ์ด๋•Œ ์ฃผ์˜ํ•  ์ ์ด ๋ฐ›์€ response์˜ data ์†์„ฑ์ด ํ•ด๋‹น ๋ถ€๋ถ„์ด๋ผ๊ณ  ๊ฒฐ๊ณผ๋ฅผ ๋ณด๋ฉฐ ํŒ๋‹จํ–ˆ๋‹ค.
import axios from "axios";

const checkGet = async () => {
    try {
      const response = await axios.get(
        "https://jsonplaceholder.typicode.com/todos/1"
      );
      console.log(response.data);
    } catch (error) {
      console.error(error);
    }
  };

ํ•ด๋‹น ๋ฒ„ํŠผ์„ ํด๋ฆญํ•˜๋ฉด ์ฝ˜์†”์— ๊ฒฐ๊ณผ๋กœ
์œ„์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ 4๊ฐ€์ง€์˜ ์†์„ฑ์„ ๊ฐ€์ง„ ๊ฒฐ๊ณผ๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

Post ์‚ฌ์šฉ๋ฒ•

fetch ์‚ฌ์šฉ๋ฒ•

์ด์™€ ๊ฐ™์€ ํ‹€์„ ์ด์šฉํ•ด postman์„ ์‚ฌ์šฉํ•œ๋‹ค.

์š”์ฒญ์— ๋”ฐ๋ฅธ ๊ฒฐ๊ณผ๊ฐ€ ์•„๋ž˜์— ๋‚˜ํƒ€๋‚œ๋‹ค

axios ์‚ฌ์šฉ๋ฒ•

const checkPost = async () => {
    try {
      const response = await axios.post(
        "https://jsonplaceholder.typicode.com/posts",
        {
          title: "misaka",
          body: "mskdaiski",
          userId: 1,
        }
      );
      console.log(response.data);
    } catch (error) {
      console.error(error);
    }
  };

fetch์— ๋น„ํ•ด์„œ (postman ์ œ์™ธ) body์— JSON.stringify๋„ ์•ˆ๋ถ™์ด๊ณ  Content-type๋„ ์ถ”๊ฐ€ํ•  ํ•„์š”๊ฐ€ ์—†๋‹ค.

ํ•ด๋‹น ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด ๊ฒฐ๊ณผ๊ฐ€ ์ฝ˜์†”์— ๋‚˜์˜ค๊ฒŒ ๋งŒ๋“ค์—ˆ๋‹ค.

PUT ์‚ฌ์šฉ๋ฒ•

fetch ์‚ฌ์šฉ๋ฒ•

id๊ฐ€ 1์ธ๊ณณ์„ update ํ•ด์ฃผ์—ˆ๋‹ค.

axios ์‚ฌ์šฉ๋ฒ•

const checkUpdate = async () => {
    try {
      const response = await axios.put(
        "https://jsonplaceholder.typicode.com/posts/1",
        {
          title: "misaka",
          body: "mskdaiski",
          userId: 1,
        }
      );
      console.log(response.data);
    } catch (error) {
      console.error(error);
    }
  };

๋งˆ์ฐฌ๊ฐ€์ง€๋กœ axios.put์„ ์ด์šฉํ•˜๊ณ  ๋”ฐ๋กœ JSON.stringify , Content-Type์„ ์ •ํ•ด์ฃผ์ง€์•Š๊ณ  ์ฒ˜๋ฆฌํ–ˆ๋‹ค.

Delete ์‚ฌ์šฉ๋ฒ•

fetch ์‚ฌ์šฉ๋ฒ•

๋‹จ์ง€ delete๋Š” ์‚ฌ์ดํŠธ์—์„œ ์‚ฌ์šฉํ•˜๋Š”๊ฑฐ๋ผ ๋ณด์ด์ง€์•Š๋Š”๋‹ค๊ณ  ํ•œ๋‹ค. (๋น„์Šทํ•˜๊ฒŒ axios.deleteํ•˜๋ฉด ๋  ๊ฑฐ๋ผ ์ƒ๊ฐ)

axios.post() / axios()

๋‘๊ฐ€์ง€ axios ์‚ฌ์šฉ๋ฒ•์ด ์žˆ๋‹ค.

axios.post()

  axios
    .post('url/login', {
      email: 'khw970421@kakao.com',
      password: 'khw970421',
    })
    .then((res) => {
      console.log(res)
    })

์ฒซ๋ฒˆ์งธ ๋งค๊ฐœ๋ณ€์ˆ˜์—๋Š” url์„
๋‘๋ฒˆ์งธ ๋งค๊ฐœ๋ณ€์ˆ˜์—๋Š” body ๋‚ด์šฉ์„
์„ธ๋ฒˆ์งธ ๋งค๊ฐœ๋ณ€์ˆ˜์—๋Š” headers ๋‚ด์šฉ์„ ์“ด๋‹ค (ํ˜„์žฌ๋Š” headers๋Š” ๋น„์–ด์žˆ๋‹ค.)
-> ๊ฐ๊ฐ์˜ ๋งค๊ฐœ๋ณ€์ˆ˜์— ๊ฐ๊ฐ ์จ์•ผํ•˜๋Š” ๊ฒƒ์„ ํ—ท๊ฐˆ๋ฆฌ๋ฉด ์•ˆ๋œ๋‹ค.

axios()

  axios({
    method: 'post',
    url: 'url/login',
    data: {
      email: 'khw970421@kakao.com',
      password: 'khw970421',
    },
    headers: { "Content-Type": "application/json" },
  }).then((res) => {
    console.log(res)
  })

์ „์ฒด๋ฅผ {}๋กœ ๋ฌถ์–ด์„œ
method,
url,
body๋ฅผ data์•ˆ์˜ ๋‚ด์šฉ์œผ๋กœ ํ•˜๊ณ 
headers๋Š” headers : {...} ํ˜•ํƒœ๋กœํ•œ๋‹ค.

ํ†ตํ•ฉ API


const getRequest = async (url, options = {}) => {
 const returnResult = await axios({
   method: 'get',
   url: `${API_END_POINT}/${url}`,
   ...options,
 })
   .then((response) => response.data)
   .catch((error) => {
     console.log(error)
   })

 if (url === 'logout') {
   message.success('๋กœ๊ทธ์•„์›ƒ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค')
   removeItem()
 }

 return returnResult
}

const postRequest = async (url, options = {}) => {
 let isProblem
 let isAdmin = false

 const returnResult = await axios({
   method: 'post',
   url: `${API_END_POINT}/${url}`,
   ...options,
 })
   .then((response) => response)
   .catch((error) => {
     console.log(error)
   })

 if (url === 'login') {
   removeItem()
   setItem('userInformation', returnResult)
   if (returnResult.data.user._id === adminId) {
     isAdmin = true
   }
   isProblem = false
   return { isProblem: isProblem, isAdmin: isAdmin }
 }

 return returnResult
}

const putRequest = async (url, options = {}) => {
 const returnResult = await axios({
   method: 'put',
   url: `${API_END_POINT}/${url}`,
   ...options,
 })
   .then((response) => response)
   .catch((error) => {
     console.log(error)
   })

 return returnResult
}

export { getRequest, postRequest, putRequest }

๊ธฐ์กด์— APIํŒŒ์ผ๋“ค์ด ๋„ˆ๋ฌด ๋งŽ์•„์„œ
์ด๋ฅผ ํ†ต์ผํ•ด์„œ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด
์ง์ ‘ Request ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์—ˆ๋‹ค.

์ฒซ๋ฒˆ์งธ ๋งค๊ฐœ๋ณ€์ˆ˜๋Š” url์„
๋‘๋ฒˆ์งธ ๋งค๊ฐœ๋ณ€์ˆ˜์—์„œ options๋กœ ์ถ”๊ฐ€์ ์ธ ํ•„์š”ํ•œ ๋‚ด์šฉ๋“ค์„ ๊ฐ€์ ธ์˜จ๋‹ค

์‚ฌ์šฉ๋ฒ•

  • Get
const { image, _id } = await getRequest('auth-user', {
        headers: {
          Authorization: BearerToken,
        },
      })
  • Post
postRequest('users/upload-photo', {
      headers: {
        Authorization: BearerToken,
      },
      data: formData,
    })
  • Put
putRequest('settings/update-password', {
      headers: {
        'Content-Type': 'application/json',
        Authorization: BearerToken,
      },
      data: {
        password: checkPw1,
      },
    })

url์„ ์ฒซ๋ฒˆ์งธ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ options๋ฅผ ํฌ๊ฒŒ {}๋กœ ๋ฌถ์€ ๋‚ด์šฉ๋“ค๋กœ ๋„ฃ๋Š”๋‹ค

+a ๋‚ด์šฉ

 const response = await axios.get(
      `https://api.github.com/search/repositories`,
      {
        params: {
          q: keyword,
          per_page: 7,
          page,
        },
      },
    );
    const response = await axios.get(
      `https://api.github.com/search/repositories?q=${keyword}&per_page=7&page=${page}`,
    );

๋‘˜๋‹ค ๊ฐ™์€ ๊ฒฐ๊ณผ์ด๋‹ค.
์ฐธ๊ณ ๋งŒ ํ•ด๋ผ

profile
๋‚˜์˜ ํ•˜๋ฃจ๋ฅผ ๊ฐ€๋Šฅํ•œ ๊ธฐ์–ตํ•˜๊ณ  ์ฆ๊ธฐ๊ณ  ํ›„ํšŒํ•˜์ง€๋ง์ž

0๊ฐœ์˜ ๋Œ“๊ธ€