Axios

De Banane Atomic
Aller à la navigationAller à la recherche

Links

Client side

Html.svg
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
<script>
    const axios = window.axios;
</script>

Instance

Javascript.svg
const instance = axios.create({
    baseURL: 'https://www.domain.net/api'
});

Authentication

Javascript.svg
const instance = axios.create({
    auth: {
        username: 'xxx',
        password: 'yyy'
    }
});

Get

Javascript.svg
instance.get('/item/1')
    .then(function (response) {
        console.log(response);
    })
    .catch(function (error) {
        console.error(error);
    });

const response = await instance.get('/item/1');

Performing multiple concurrent requests

Javascript.svg
const calls = [];
calls.push(instance.get('/item/1'));
calls.push(instance.get('/item/2'));

const responses = await Promise.all(calls);