컴퓨터 정보/리액트

api 불러올때 fetch 말고 axios

혜닝혜루 2022. 10. 28. 20:35
728x90
반응형

https://www.npmjs.com/package/axios

 

axios

Promise based HTTP client for the browser and node.js. Latest version: 1.1.3, last published: 13 days ago. Start using axios in your project by running `npm i axios`. There are 88325 other projects in the npm registry using axios.

www.npmjs.com

홈페이지 참고

 

 

npm

npm install axios

사용방법

axios.get('/user?ID=12345')

respone을 get으로 붙혀주고 http가아닌 가져오고 싶은 뒷부분만 가져온다.

axios.post('/user?ID=12345')

데이터를 보내는 작업은 . post로 해주면 된다.

 

 

이걸로 쓰시면 됩니다.

 

const instance = axios.create

({ baseURL: 'https://some-domain.com/api/', > 맨앞에 동일하게 사용되는 url 붙혀주기

timeout: 1000, > 1초가 지날때까지 응답이 없으면 에러인걸로 하자 

headers: {'X-Custom-Header': 'foobar'} }); -> api 기본적으로 들어가는 헤더값들

 

 

728x90