How to use axios

Comprehensive axios code examples:

How to use axios.isAxiosError:

278
279
280
281
282
283
284
285
286
287
    }
    action = "drop";
  }
} catch (error) {
  if (
    isAxiosError(error) &&
    error.response &&
    error.response.status === 404
  ) {
    //action = "drop";

How to use axios.CancelToken:

74
75
76
77
78
79
80
81
82
83
request(opts, clbk) {
	var response = null;

	var error = null;

	const cancelTokenSource = axios.CancelToken.source();

	var aopts = {
		url: opts.uri,
		params: opts.qs,

How to use axios.Axios:

525
526
527
528
529
530
531
532
533
534
async _downloadCacheZip(options) {
    options = bindOptions({
        ghproxy: false,
    }, options);

    const axios = new Axios({});

    const url = (options.ghproxy ? "https://ghproxy.com/" : "") + "https://raw.githubusercontent.com/yuko1101/enka-network-api/main/cache.zip";

    const res = await axios.get(url, {

How to use axios.options:

54
55
56
57
58
59
60
61
62
63
}

async options(url, headers, config = {}) {
        config.headers = headers;
        config = this._checkConfig(config);
        return axios.options(url, config)
}

async post(url, body, headers, config = {}) {
        config.headers = headers;

How to use axios.defaults:

35
36
37
38
39
40
41
42
43
});
it('should not modify global defaults in axios', function () {
  // https://github.com/slackapi/node-slack-sdk/issues/1037
  const client = new WebClient();

  const globalDefault = axios.defaults.headers.post['Content-Type'];
  // The axios.default's defaults should not be modified.
  // Specifically, defaults.headers.post should be kept as-is
  assert.exists(globalDefault);

How to use axios.patch:

73
74
75
76
77
78
79
80
81
        }

        async patch(url, body, headers, config = {}) {
                config.headers = headers;
                config = this._checkConfig(config);
                return axios.patch(url, body, config)
        }

}

How to use axios.delete:

37
38
39
40
41
42
43
44
45
46
  },
};

try {
  const { headers, data } = config;
  const uninstallPlugin = await axios.delete(config.url, { headers, data });
  const { data: response } = uninstallPlugin;
  return response;
} catch (error) {
  if (error.isAxiosError) {

How to use axios.all:

1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
var view = req.params.view
var entry = req.params.entry

console.log('/manage/entry/amend/' + view + '/' + id + '/' + entry)

axios.all([getDataByID(id), getPerson(entry), getArtefact(entry)]).then(
  axios.spread((entryx, person, artefact) => {
    entry = entryx[0]
    res.render('manage/entry/amend/submissionvalue.html', {
      entry,

How to use axios.spread:

953
954
955
956
957
958
959
960
961
962
  axios.get(_url(FORMS_ENDPOINT, name, last_sync_time)),
  axios.get(_url(LISTS_ENDPOINT, name, last_sync_time)),
  axios.get(_url(FORM_CHOICE_ENDPOINT, name, last_sync_time)),
])
.then(
  axios.spread((formListRes, listRes, formChoice) => {
    const newLayoutQuery = db.prepare('INSERT INTO app_log(time) VALUES(?)');
    newLayoutQuery.run(Date.now());
    if (formListRes.data) {
      electronLog.info(

How to use axios.default:

68
69
70
71
72
73
74
75
76
77
let response = yield querier.sendGetRequest(new normalisedURLPath_1.default("/telemetry"), {});
let telemetryId;
if (response.exists) {
    telemetryId = response.telemetryId;
}
yield axios_1.default({
    method: "POST",
    url: "https://api.supertokens.com/0/st/telemetry",
    data: {
        appName: this.appInfo.appName,

How to use axios.head:

48
49
50
51
52
53
54
55
56
57
}

async head(url, headers, config = {}) {
        config.headers = headers;
        config = this._checkConfig(config);
        return axios.head(url, config)
}

async options(url, headers, config = {}) {
        config.headers = headers;

How to use axios.put:

67
68
69
70
71
72
73
74
75
76
}

async put(url, body, headers, config = {}) {
        config.headers = headers;
        config = this._checkConfig(config);
        return axios.put(url, body, config)
}

async patch(url, body, headers, config = {}) {
        config.headers = headers;

How to use axios.request:

30
31
32
33
34
35
36
37
38
39

async request(body, headers, config = {}) {
        config.data = body;
        config.headers = headers;
        config = this._checkConfig(config);
        return axios.request(config)
}

async get(url, headers, config = {}) {
        config.headers = headers;

How to use axios.create:

81
82
83
84
85
86
87
88
89
90
}

actions.debug('Instance Configuration: ' + JSON.stringify(instanceConfig))

/** @type {axios.AxiosInstance} */
const instance = axios.create(instanceConfig);

actions.debug('Request Data: ' + JSON.stringify(requestData))

const execRequest = async () => {

How to use axios.post:

14
15
16
17
18
19
20
21
22
23
}

async postToSlack({ message }) {
  try {
    if (!this.slackUrl) return
    await axios.post(this.slackUrl, { text: message })
  } catch (e) {
    this.childLogger.info(
      `Error posting to slack in slack reporter ${e.toString()}`
    )

How to use axios.get:

50
51
52
53
54
55
56
57
58
59

// axios timeout 只针对 response,不针对 connection,因此需要二重保险
let timeout = axiosOption.timeout ? setTimeout(() => controller.abort(), axiosOption.timeout * 2) : null;

try {
  const res = await Axios.get(finalUrl.href, axiosOption);
  if (timeout) {
    clearTimeout(timeout);
    timeout = null;
  }