๐ Axios๋?
๋ธ๋ผ์ฐ์ , Node.js๋ฅผ ์ํ Promise API๋ฅผ ํ์ฉํ๋ HTTP ๋น๋๊ธฐ ํต์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ด๋ค. ๋ฐฑ์๋์ ํ๋ก ํธ์๋๋ ํต์ ์ ์ฝ๊ฒํ๊ธฐ ์ํด Ajax์ ๋๋ถ์ด ์ฌ์ฉํ๋ค.
๐ Axios์ ํน์ง
- ์ด์ ํ๊ฒฝ์ ๋ฐ๋ผ ๋ธ๋ผ์ฐ์ ์ XMLHttpRequest ๊ฐ์ฒด ๋๋ Node.js์ http api ์ฌ์ฉ
- Promise(ES6) API ์ฌ์ฉ
- ์์ฒญ๊ณผ ์๋ต ๋ฐ์ดํฐ์ ๋ณํ
- HTTP ์์ฒญ ์ทจ์
- HTTP ์์ฒญ๊ณผ ์๋ต์ JSON ํํ๋ก ์๋ ๋ณ๊ฒฝ
๐ Axios ์ค์นํ๊ธฐ
> npm install axios
๐ Axios์ ๋ฌธ๋ฒ ๊ตฌ์ฑ
axios({
url: 'https://test/api/cafe/list/today', // ํต์ ํ ์น๋ฌธ์
method: 'get', // ํต์ ํ ๋ฐฉ์
data: { // ์ธ์๋ก ๋ณด๋ผ ๋ฐ์ดํฐ
foo: 'diary'
}
});
๐ Axios ์์ฒญ(request) ํ๋ผ๋ฏธํฐ ๋ฌธ๋ฒ ์์
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) {
return status >= 200 && status < 300; // default
}, // HTTP์๋ต ์ํ ์ฝ๋์ ๋ํด promise์ ๋ฐํ ๊ฐ์ด resolve ๋๋ reject ํ ์ง ์ง์
proxy: {
host: '127.0.0.1',
port: 9000,
auth: {
username: 'mikeymike',
password: 'rapunz3l'
}
}, // proxy์๋ฒ์ hostname๊ณผ port๋ฅผ ์ ์
maxRedirects: 5, // node.js์์ ์ฌ์ฉ๋๋ ๋ฆฌ๋ค์ด๋ ํธ ์ต๋์น๋ฅผ ์ง์
httpsAgent: new https.Agent({ keepAlive: true }), // node.js์์ https๋ฅผ ์์ฒญ์ ํ ๋ ์ฌ์ฉ์ ์ ์ agent๋ฅผ ์ ์
})
.then(function (response) {
// response Action
});
- method : ์์ฒญ๋ฐฉ์. (get์ด ๋ํดํธ)
- url : ์๋ฒ ์ฃผ์
- baseURL : url์ ์๋๊ฒฝ๋ก๋ก ์ธ๋ url ๋งจ ์์ ๋ถ๋ ์ฃผ์.
- ์๋ฅผ๋ค์ด, url์ด /post ์ด๊ณ baseURL์ด https://some-domain.com/api/ ์ด๋ฉด,
https://some-domain.com/api/post๋ก ์์ฒญ ๊ฐ๊ฒ ๋๋ค.
- ์๋ฅผ๋ค์ด, url์ด /post ์ด๊ณ baseURL์ด https://some-domain.com/api/ ์ด๋ฉด,
- headers : ์์ฒญ ํค๋
- data : ์์ฒญ ๋ฐฉ์์ด 'PUT', 'POST', 'PATCH' ํด๋นํ๋ ๊ฒฝ์ฐ body์ ๋ณด๋ด๋ ๋ฐ์ดํฐ
- params : URL ํ๋ผ๋ฏธํฐ ( ?key=value ๋ก ์์ฒญํ๋ url get๋ฐฉ์์ ๊ฐ์ฒด๋ก ํํํ ๊ฒ)
- timeout : ์์ฒญ timeout์ด ๋ฐ๋ ๋๊ธฐ ์ milliseconds์ ์๊ฐ์ ์์ฒญ. timeout ๋ณด๋ค ์์ฒญ์ด ๊ธธ์ด์ง๋ค๋ฉด, ์์ฒญ์ ์ทจ์๋๊ฒ ๋๋ค.
- responseType : ์๋ฒ๊ฐ ์๋ตํด์ฃผ๋ ๋ฐ์ดํฐ์ ํ์ ์ง์ (arraybuffer, documetn, json, text, stream, blob)
- responseEncoding : ๋์ฝ๋ฉ ์๋ต์ ์ฌ์ฉํ๊ธฐ ์ํ ์ธ์ฝ๋ฉ (utf-8)
- transformRequest : ์๋ฒ์ ์ ๋ฌ๋๊ธฐ ์ ์ ์์ฒญ ๋ฐ์ดํฐ๋ฅผ ๋ฐ๊ฟ ์ ์๋ค.
- ์์ฒญ ๋ฐฉ์ 'PUT', 'POST', 'PATCH', 'DELETE' ์ ํด๋นํ๋ ๊ฒฝ์ฐ์ ์ด์ฉ ๊ฐ๋ฅ
- ๋ฐฐ์ด์ ๋ง์ง๋ง ํจ์๋ string ๋๋ Buffer, ๋๋ ArrayBuffer๋ฅผ ๋ฐํํฉ
- header ๊ฐ์ฒด๋ฅผ ์์ ๊ฐ๋ฅ
- ์์ฒญ ๋ฐฉ์ 'PUT', 'POST', 'PATCH', 'DELETE' ์ ํด๋นํ๋ ๊ฒฝ์ฐ์ ์ด์ฉ ๊ฐ๋ฅ
- transformResponse : ์๋ต ๋ฐ์ดํฐ๊ฐ ๋ง๋ค์ด์ง๊ธฐ ์ ์ ๋ณํ ๊ฐ๋ฅ
- withCredentials : cross-site access-control ์์ฒญ์ ํ์ฉ ์ ๋ฌด. ์ด๋ฅผ true๋ก ํ๋ฉด cross-origin์ผ๋ก ์ฟ ํค๊ฐ์ ์ ๋ฌ ํ ์ ์๋ค.
- auth : HTTP์ ๊ธฐ๋ณธ ์ธ์ฆ์ ์ฌ์ฉ. auth๋ฅผ ํตํด์ HTTP์ ๊ธฐ๋ณธ ์ธ์ฆ์ด ๊ตฌ์ฑ์ด ๊ฐ๋ฅ
- maxContentLength: http ์๋ต ๋ด์ฉ์ max ์ฌ์ด์ฆ๋ฅผ ์ง์ ํ๋๋ก ํ๋ ์ต์
- validateStatus : HTTP์๋ต ์ํ ์ฝ๋์ ๋ํด promise์ ๋ฐํ ๊ฐ์ด resolve ๋๋ reject ํ ์ง ์ง์ ํ๋๋ก ํ๋ ์ต์
- maxRedirects : node.js์์ ์ฌ์ฉ๋๋ ๋ฆฌ๋ค์ด๋ ํธ ์ต๋์น๋ฅผ ์ง์
- httpAgent / httpsAgent : node.js์์ http๋ https๋ฅผ ์์ฒญ์ ํ ๋ ์ฌ์ฉ์ ์ ์ agent๋ฅผ ์ ์ํ๋ ์ต์
- proxy : proxy์๋ฒ์ hostname๊ณผ port๋ฅผ ์ ์ํ๋ ์ต์
- cancelToken : ์์ฒญ์ ์ทจ์ํ๋๋ฐ ์ฌ์ฉ๋์ด ์ง๋ ์ทจ์ํ ํฐ์ ๋ช ์
๐ Axios ์๋ต(response) ํ๋ผ๋ฏธํฐ ๋ฌธ๋ฒ ์์
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)
})
axios๋ฅผ ํตํด ์์ฒญ์ ์๋ฒ์๊ฒ ๋ณด๋ด๋ฉด, ์๋ฒ์์ ์ฒ๋ฆฌ๋ฅผํ๊ณ ๋ค์ ๋ฐ์ดํฐ๋ฅผ ํด๋ผ์ด์ธํธ์ ์๋ต ํ๊ฒ ๋๋ค.
์ด๋ฅผ .then์ผ๋ก ํจ์์ธ์๋ก(response)๋ก ๋ฐ์ ๊ฐ์ฒด์ ๋ด์ง ๋ฐ์ดํฐ๊ฐ ๋ฐ๋ก ์๋ต ๋ฐ์ดํฐ์ด๋ค.
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 ์ธ์คํด์ค(๋ฆฌ๋๋ ์
)
๐ Axios ๋จ์ถ ๋ฉ์๋
- GET : axios.get(url[, config])
- POST : axios.post(url, data[, config])
- PUT : axios.put(url, data[, config])
- DELETE : axios.delete(url[, config])
์ถ์ฒ:https://inpa.tistory.com/entry/AXIOS-%F0%9F%93%9A-%EC%84%A4%EC%B9%98-%EC%82%AC%EC%9A%A9
'WIL' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
ํ์ ์งํ ์ํฉ (0) | 2023.05.22 |
---|---|
Mini Project (0) | 2023.05.14 |
React_Life Cycle (0) | 2023.04.30 |
React_State, Props (1) | 2023.04.23 |
DOM๊ณผ Virtual DOM, props์ state (0) | 2023.04.17 |