Frontend
Axios에 대해서
구라미
2020. 1. 16. 19:30
Axios는
Axios는 뷰에서 권고하는 Promise 기반의 HTTP 통신 라이브러리이다. HTTP 클라이언트 라이브러리이다.
https://github.com/axios/axios#example
axios/axios
Promise based HTTP client for the browser and node.js - axios/axios
github.com
features
- 브라우저로부터 XMLHttpRequests를 만들어낸다.
- node.js로부터 http requests를 만들어냄
- Promise API를 지원한다.
- request와 response를 인터셉트
Axios 사용방법
Axios 라이브러리를 설치하고 나면 axios라는 변수에 접근할 수 있다.
Axios로 GET request 동작하기
const axios = require('axios');
// Make a request for a user with a given ID
axios.get('/user?ID=12345')
.then(function (response) {
// handle success
console.log(response);
})
.catch(function (error) {
// handle error
console.log(error);
})
.finally(function () {
// always executed
});
// Optionally the request above could also be done as
axios.get('/user', {
params: {
ID: 12345
}
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
})
.finally(function () {
// always executed
});
// Want to use async/await? Add the `async` keyword to your outer function/method.
async function getUser() {
try {
const response = await axios.get('/user?ID=12345');
console.log(response);
} catch (error) {
console.error(error);
}
}
POST request 동작하기
axios.post('/user', {
firstName: 'Fred',
lastName: 'Flintstone'
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});