axios
20시에 슉하고 사라졌다가 다음날 5시에 챡하고 나타났는데, 왜이리 졸리지? ㅡㅡ?
1. Axios
HTTP 비동기 통신 라이브러리, 써드파티
FetchAPI | Axios |
설치필요 | 빌트인 |
자동 JSON변환 | .json() 변환 |
const PROTOCOL = "https://";
const HOST = "host.com";
const ENDPOINT = "/endpoint";
const url = PROTOCOL + HOST + ENDPOINT;
//--------------------------------------
//Just Promise
fetch(url, { method: "GET" })
.then((response) => response.json())
.then((json) => console.log(json))
.catch((error) => console.log(error));
//Just Async/Await
async function request() {
const response = await fetch(url, {
method: "GET",
});
const data = await response.json();
console.log(data);
}
request();
//------------------------------------
import axios from "axios";
//Promise ver
axios
.get(url)
.then((response) => {
const { data } = response;
console.log(data);
})
.catch((error) => console.log(error));
//Async / Await ver
async function request() {
const response = await axios.get(url);
const { data } = response;
console.log(data);
}
request();
//axios 사용하지 않을 시 object
//axios 사용시 Object