Axios란?
Axios는 node.js와 브라우저를 위한 Promise 기반 HTTP 클라이언트 입니다.
그것은 동형입니다(동일한 코드베이스로 브라우저와 node.js에서 실행할 수 있습니다.)
서버 사이드에서는 네이티브 node.js의 http 모듈을 사용하고, 클라이언트(브라우저)에서는
XMLHttpRequests를 사용합니다.AXIOS 소개 | https://axios-http.com/kr/docs/intro
axios({
url: 'https://test/api/cafe/list/today', // 통신할 웹문서
method: 'get', // 통신할 방식
data: {
foo: 'diary' // 인자로 보낼 데이터
}
});
axios({
method: 'get', // 통신 방식
url: 'www.naver.com', // 서버
headers: { 'X-Requested-With': 'XMLHttpRequest' }, // 요청 헤더 설정
params: { api_key: '1234', langualge: 'en' }, // ?파라미터를 전달
responseType: 'json', // default
maxContentLength: 2000, // http 응답 내용의 max 사이즈
validateStatus: function(status) { // default
return status >= 200 && status < 300;
}, // http 응답 상태 코드에 대해 promise의 반환 값이 resolve 또는 reject 할지 지정
proxy: {
host: '127.0.0.1',
port: 9000,
auth: {
username: 'username',
password: 'passworld',
}
}, // proxy 서버의 hostname 과 port 를 정의
maxRedirects: 5, // node.js 에서 사용되는 리다이렉트 최대치를 지정
httpsAgent: new https.Agent({ keepAlive: true }),
// node.js 에서 https 를 요청을 할 때 사용자 정의 agent 를 정의
})
.then(function(response){
// response Action
})
/post 이고 baseURL이 https://some-domain.com/api/ 이면,PUT, POST, PATCH 해당하는 경우 body에 보내는 데이터PUT, POST, PATCH, DELETE 에 해당하는 경우에 이용 가능axios 를 통해 요청을 서버에게 보내면, 서버에서 처리를 하고 다시 데이터를 클라이언트에 응답 하게 된다.
이를 .then 으로 함수인자료(response)로 받아 객체에 담긴 데이터가 바로 응답 데이터다.
ajax 를 통해 서버로부터 받는 응답의 정보는 다음과 같다.
axios({
method: 'get',
url: 'www.naver.com',
})
.then(function(response) {
console.log(response.data)
console.log(response.status)
console.log(response.statusText)
console.log(response.headers)
console.log(response.config)
})
response.data: {}, // 서버가 제공한 응답 (데이터)
response.status: 200, // 'status' 는 서버 응답의 http 상태 코드
response.statusText: 'OK', // 'statusText'는 서버 응답으로 부터의 http 상태 메세지
response.headers: {}, // 'headers' 서버가 응답 한 헤더는 모든 헤더 이름이 소문자로 제공
response.config: {}, // 'config' 는 요청에 대해 'axios' 에 설정된 구성 (config)
response.request: {} // 'request' 는 응답을 생성한 요청
// 브라우저 : XMLHttpRequest 인스턴스
// Node.js : ClientRequest 인스턴스 (리디렉션)
const instance = axios.create({
baseURL: 'https://some-domain.com/api/',
timeout: 1000,
headers: {'X-Custom-Header': 'foobar'}
});
axios.request(config)
axios.get(url[,config])
axios.delete(url[,config])
axios.head(url[,config])
axios.options(url[,config])
axios.post(url[,config])
axios.put(url[,config])
axios.patch(url[,config])
axios.getUri(url[,config])
댓글