alias

axios.request(config) axios.get(url[, config]) axios.delete(url[, config]) axios.head(url[, config]) axios.options(url[, config]) axios.post(url[, data[, config]]) axios.put(url[, data[, config]]) axios.patch(url[, data[, config]])

Config Options

url is required, and get is the default method

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
{
url: 'string',
method: 'get',
baseUrl: 'https://some-domain.com/api/', // prepended to url unless url is absolute,
transformRequest: [function (data) {
return data
}],
transformResponse: [function (data) {
return data
}],
headers: {
'X-Requested-With': 'XMLHttpRequest',
},
params: { // url params to be sent with the request, plain object or URLSearchParams Object
ID: 12345,
},
paramsSerializer: function (params) {
return Qs.stringify(params, { arrayFormat: 'brackets' })
},
data: { // the data sent as request body, only applicable for request method 'PUT', 'POST', 'PATCH'. When no transformRequest is set, must be one of the following types: string, plain object, ArrayBuffer, ArrayBufferView, URLSearchParams, FormData(Browser only), File(browser only), Blob(browser only), Stream(Node only), Buffer(Node only)
firstName: 'fred',
},
timeout: 1000,
withCredentials: false // default
adapter: function (config) { // allows custom handling of requests which makes testing easier.

},
auth: {
username: 'janedoe',
password: '...',
},
responseType: 'json', // default
xsrfCookieName: 'XSRF-TOKEN', // defualt
onUploadProgress: function (progressEvent) {

},
onDownloadProgress: function(progressEvent) {

},
maxContentLength: 2000,
validateStatus: function (status) {
return status >= 200 && status < 300 // default
},
maxRedirects: 5, // default
httpAgent: new http.Agent({ keepAlive: true }),
httpsAgent: new http.Agent({ keepAlive: true }),
proxy: { // defines the hostname and port of the proxy server
host: '127.0.0.1',
port: 9000,
auth: {
username: '...',
password: '...',
}
},
cancelToken: new CancelToken(function (cancel) {

})
}

response Schema

1
2
3
4
5
6
7
8
{
data: {},
status: 200,
statusText: 'OK',
headers: {},
config: {},
request: {},
}

errors

1
2
3
4
5
6
7
8
9
axios.get('/user/123').catch((err) => {
if (err.response) {
// err.response.data
// err.response.status
// err.response.headers
} else if (res.request) {
} else {
}
})

get

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
axios
.get('/user?ID=12345')
.then((res) => {
console.log(res)
})
.catch((err) => {
console.log(err)
})

axios
.get('/user', {
params: {
ID: 12345,
},
})
.then((res) => {
console.log(res)
})
.catch((err) => {
console.log(err)
})

post

1
2
3
4
5
6
7
8
9
10
11
axios
.post('/user', {
firstName: 'Fred',
lastName: 'Flintstrong',
})
.then((res) => {
console.log(res)
})
.catch((err) => {
console.log(err)
})

concurrency

1
2
3
4
5
6
7
8
9
10
11
12
13
function getUserAccount() {
return axios.get('/user/12345')
}

function getUserPermissions() {
return axios.get('/user/12345/permissions')
}

axios.all([getUserAccount(), getUserPermissions()]).then(
axios.spread(function (acct, perms) {
// Both request are now complete
}),
)

config

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
axios({
method: 'post',
url: '/user/12345',
data: {
firstName: 'Fred',
lastName: 'Flintstone',
},
})

axios({
method: 'get',
url: 'http://bit.ly/2mTM3n',
responseType: 'stream',
}).then((res) => {
res.data.pipe(fs.createWriteStream('ada_lovelace.jpg'))
})