How to use the defaults function from axios

Find comprehensive JavaScript axios.defaults code examples handpicked from public code repositorys.

axios.defaults is an object that contains the default configurations for all Axios requests.

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);
fork icon664
star icon0
watch icon130

0
1
2
3
4
5
6
7
8
9
10
11
const fs = require("fs");
const yaml = require("js-yaml");
const axios = require("axios");


// Using a token is optional, but will greatly reduce the chance of hitting the rate limit
if (process.env.GITHUB_TOKEN) axios.defaults.headers.common["Authorization"] = `Bearer ${process.env.GITHUB_TOKEN}`;


(async () => {
    // Debug
    console.log(`PR Number: ${process.env.PR_NUMBER}`);
fork icon13
star icon5
watch icon1

+ 4 other calls in file

How does axios.defaults work?

axios.defaults is an object that defines the default settings for every HTTP request made using the Axios library, such as the base URL, headers, timeout, and other request options. When making requests, these defaults are merged with the request-specific options, allowing for easy configuration and consistency across all requests.

13
14
15
16
17
18
19
20
21
22
23
24
25


const REGISTER_APPLICATION_TIMEOUT = 90000 // 1 minute 30
const SESSION_TIMEOUT = 600000; // 10 minutes


module.exports = function (RED) {
  axios.defaults.httpsAgent = new https.Agent({ rejectUnauthorized: false });


  class FreeboxServerNode {
    constructor(config) {
      RED.nodes.createNode(this, config);
fork icon3
star icon1
watch icon2

+ 4 other calls in file

311
312
313
314
315
316
317
318
319
320

// },
// AddingTwoCommentsToEvents: async (eve, partner, admin) => {
//     let updatedEvent = "";
//     try {
//         axios.defaults.adapter = require('axios/lib/adapters/http');
//         await axios.post(`http://localhost:3000/api/event/${eve._id}/comment`, {
//             userType: "Admin",
//             userId: admin._id,
//             comment: "it wasn't as good as expected"
fork icon0
star icon3
watch icon5

+ 9 other calls in file

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// import the axios library
const axios = require("axios");

// set the default URL and headers
axios.defaults.baseURL = "https://jsonplaceholder.typicode.com";
axios.defaults.headers.common["Authorization"] = "Bearer your_token_here";

// make a GET request using the default configurations
axios
  .get("/posts")
  .then((response) => {
    console.log(response.data);
  })
  .catch((error) => {
    console.error(error);
  });

In this example, we set the default base URL for our requests to the https://jsonplaceholder.typicode.com API and add a default authorization header to all requests using the Authorization key. We then make a GET request to /posts endpoint, which uses the default base URL and headers that we set earlier.

37
38
39
40
41
42
43
44
45
46
    if (error.response != undefined 
        && (error.response.status == 401 && !originalRequest._retry)) 
    {
        originalRequest._retry = true;
        const accessToken = await getApiAuthenticationToken(axiosInstance) || ''          
        axios.defaults.headers.common['Authorization'] = `Bearer ${accessToken}`
        return axiosInstance(originalRequest);
    }
    return Promise.reject(error)
})
fork icon0
star icon0
watch icon1