Axios config default GET param

axios interceptors
axios withcredentials
axios authorization header
axios post headers
axios default post params
axios basic auth
axios cheat sheet
axios tutorial

I'm using Axios in my VueJS application and I want to add a default GET param in my request. I send my API-KEY through the URL ?api-key=secret and I don't want to specify this parameter each time.

I see in the documentation that we can set Global custom defaults. With that we don't have to specify the header each time. But can we do the same for get param ?

Here it is:

axios.defaults.params = {}
axios.defaults.params['api-key'] = secret

Support for axios.default.params? · Issue #229 · axios/axios · GitHub, Summary Is there a way to set default params to all type of request methods? I check this const api = axios.create({ baseURL: config.api.baseURL And I must say I'm not overriding the params when performing a query. Teams. Q&A for Work. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information.

If you need to call a function before each axios request, you should use an interceptor.

Setting default params · Issue #1476 · axios/axios · GitHub, Make a request for a user with a given ID axios.get('/user?ID=12345') Set config defaults when creating the instance var instance = axios.create({ baseURL​:  // Create an instance using the config defaults provided by the library // At this point the timeout config value is `0` as is the default for the library const instance = axios. create (); // Override timeout default for the library // Now all requests using this instance will wait 2.5 seconds before timing out instance. defaults. timeout

I struggled to get this to work with axios instances using the two most commonly suggested methods:

axios.defaults.params = {}
axios.defaults.params['api-key'] = secret

and using the params attribute:

const axClient = axios.create({
    baseURL: process.env.VUE_APP_BASE_URL,
    params: {
        api-key: process.env.VUE_APP_API_KEY
    }
});

I did manage to get it working nicely using interceptors like this:

const axClient = axios.create({
    baseURL: process.env.VUE_APP_BASE_URL,
});

axClient.interceptors.request.use((config) => {
    config.params = config.params || {};
    config.params['api-key'] = process.env.VUE_APP_API_KEY;
    return config;
});

Axios Cheat Sheet, Requests will default to GET if method is not specified. then defaults property of the instance, and finally config argument for the request. Is there any way to get support for axios.default.params? Does this exist already? I really love the deafult functionality for headers, I think it makes a lot of sense.

Request Config, axios.request(config); axios.get(url[, config]); axios.delete(url[, config]) If the promise is fulfilled, the first argument of then() will be called; if the By default, Axios automatically converts requests and responses to JSON. Axios tutorial shows how to generage requests in JavaScript using Axios client library. Axios is a promise based HTTP client for the browser and Node.js.

How to make HTTP requests like a pro with Axios, Axios Module for Nuxt. nuxt.config.js Default value of prefix is / . Environment variable API_URL can be used to override baseURL . using the progress option in an inline request configuration: this.$axios.$get('URL', { progress: false })  In Axios, you can create POST requests using the .post() method and passing the data as a second parameter. Here’s the method’s signature for your reference. axios.post(url[, data[, config]]) Using the mocked endpoint above, our POST request would become:

Options, Send a GET request (default method) axios('/user/12345'); If you are using environment variables // for your proxy configuration, you can also define a  Axios is a Promise-based HTTP client for JavaScript which can be used in your front-end application and in your Node.js backend. By using Axios it’s easy to send asynchronous HTTP request to REST…

Comments
  • Do you want to add it to the headers or just to every get request?
  • @Taylor every requests. Not inside the header
  • Marcelo's answer should work or you could use interceptors which works like middleware
  • It will not be replaced by the other get param when I'll use axios ?
  • No, it will keep the default params and append the other ones you pass.
  • Ok thanks, I'll check that tomorrow and I'll accept the answer
  • Ok, let me know if it doesn't work
  • Why use interceptor instead of default ?
  • Maybe you need to check the users token before appending the secret