Axios Headers



  • Hello guys im having trouble setting axios headers to get token from localstorage,which file do i set that



  • I set the default properites for axios in the boot file set up by Quasar CLI under src/boot/axios.js. Place the code inside the exported default function.

    export default async ({ Vue }) => {
      axios.defaults.headers.common['Accept'] = 'application/json';
    
      Vue.prototype.$axios = axios
    }
    


  • @nqaba
    In my case this file is under
    /src/plugins/axios.js

    import { LocalStorage } from 'quasar'; // make sure you edit quasar.conf.js
    import axios from 'axios';
    
    export default ({ app, router, Vue }) => {
        axios.defaults.baseURL = 'https://api.somewhere.com/';
    
        // Add a response interceptor
        axios.interceptors.response.use(function (response) {
            // Do something with response data
            // Loading.hide();
            return response;
        }, function (error) {
            // Do something with response error
            return Promise.reject(error);
        });
    
        axios.interceptors.request.use(function (config) {
            // default options
            //  Loading.show();
            return config;
        });
    
        axios.defaults.method = 'POST';
    
        axios.defaults.transformRequest = [function (data, headers) {
            // Do whatever you want to transform the data
            return data;
        }];
    
        // axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
        axios.defaults.headers.post['Content-Type'] = 'application/json';
        // axios.defaults.headers.common['Authorization'] = AUTH_TOKEN;
    
        Vue.prototype.$axios = axios;
        // window.$axios = axios; super super super global : )
    };
    
    

    in quasar.conf.js

                // Quasar plugins
                plugins: [
                    'Notify',
                    'SessionStorage',
                    'LocalStorage', /** this line **/
                    'Dialog',
    


  • @nqaba import LocalStorage in your src/plugins/axios.js / src/boot/axios.js (in v1) and set the header there same as what @turigeza posted above.



  • Thanks a lot guys



  • This post is deleted!

Log in to reply