No More Posting New Topics!

If you have a question or an issue, please start a thread in our Github Discussions Forum.
This forum is closed for new threads/ topics.

Navigation

    Quasar Framework

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search

    Axios Headers

    Help
    5
    6
    4001
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • nqaba
      nqaba last edited by

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

      T 1 Reply Last reply Reply Quote 0
      • D
        dannyalder88 last edited by

        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
        }
        
        1 Reply Last reply Reply Quote 0
        • T
          turigeza @nqaba last edited by turigeza

          @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',
          
          1 Reply Last reply Reply Quote 1
          • metalsadman
            metalsadman last edited by

            @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.

            P 1 Reply Last reply Reply Quote 0
            • nqaba
              nqaba last edited by

              Thanks a lot guys

              1 Reply Last reply Reply Quote 0
              • P
                pureblood @metalsadman last edited by

                This post is deleted!
                1 Reply Last reply Reply Quote 0
                • First post
                  Last post