2021-01-29 00:20:46 +05:30
---
stage: none
group: unassigned
2022-11-25 23:54:43 +05:30
info: To determine the technical writer assigned to the Stage/Group associated with this page, see https://about.gitlab.com/handbook/product/ux/technical-writing/#assignments
2021-01-29 00:20:46 +05:30
---
2018-03-17 18:26:18 +05:30
# Axios
2019-10-12 21:52:04 +05:30
2020-06-23 00:09:42 +05:30
We use [Axios ](https://github.com/axios/axios ) to communicate with the server in Vue applications and most new code.
2018-03-17 18:26:18 +05:30
2020-06-23 00:09:42 +05:30
In order to guarantee all defaults are set you *should not use Axios directly* , you should import Axios from `axios_utils` .
2018-03-17 18:26:18 +05:30
## CSRF token
2019-10-12 21:52:04 +05:30
2020-06-23 00:09:42 +05:30
All our requests require a CSRF token.
To guarantee this token is set, we are importing [Axios ](https://github.com/axios/axios ), setting the token, and exporting `axios` .
2018-03-17 18:26:18 +05:30
2020-06-23 00:09:42 +05:30
This exported module should be used instead of directly using Axios to ensure the token is set.
2018-03-17 18:26:18 +05:30
## Usage
2019-10-12 21:52:04 +05:30
2018-03-17 18:26:18 +05:30
```javascript
import axios from './lib/utils/axios_utils';
axios.get(url)
.then((response) => {
// `data` is the response that was provided by the server
const data = response.data;
// `headers` the headers that the server responded with
// All header names are lower cased
const paginationData = response.headers;
})
.catch(() => {
//handle the error
});
```
2020-06-23 00:09:42 +05:30
## Mock Axios response in tests
2018-03-17 18:26:18 +05:30
2020-05-24 23:13:21 +05:30
To help us mock the responses we are using [axios-mock-adapter ](https://github.com/ctimmerm/axios-mock-adapter ).
2018-03-17 18:26:18 +05:30
2020-05-24 23:13:21 +05:30
Advantages over [`spyOn()` ](https://jasmine.github.io/api/edge/global.html#spyOn ):
2018-03-17 18:26:18 +05:30
- no need to create response objects
- does not allow call through (which we want to avoid)
2021-03-11 19:13:27 +05:30
- clear API to test error cases
2018-03-17 18:26:18 +05:30
- provides `replyOnce()` to allow for different responses
2020-06-23 00:09:42 +05:30
We have also decided against using [Axios interceptors ](https://github.com/axios/axios#interceptors ) because they are not suitable for mocking.
2018-03-17 18:26:18 +05:30
### Example
```javascript
import axios from '~/lib/utils/axios_utils';
import MockAdapter from 'axios-mock-adapter';
let mock;
beforeEach(() => {
// This sets the mock adapter on the default instance
mock = new MockAdapter(axios);
// Mock any GET request to /users
// arguments for reply are (status, data, headers)
mock.onGet('/users').reply(200, {
users: [
{ id: 1, name: 'John Smith' }
]
});
});
afterEach(() => {
mock.restore();
});
```
2020-06-23 00:09:42 +05:30
### Mock poll requests in tests with Axios
2018-03-17 18:26:18 +05:30
2021-11-11 11:23:49 +05:30
Because a polling function requires a header object, we need to always include an object as the third argument:
2018-03-17 18:26:18 +05:30
```javascript
mock.onGet('/users').reply(200, { foo: 'bar' }, {});
```