axios by itself comes with two useful "methods" the interceptors that are none but middlewares between the request and the response. so if on each request you want to send the token. use the interceptor.request.

i made apackage that helps you out:

$ npm i axios-es6-class

now you can use axios as class

export class userapi extends api {
    constructor (config) {

        // this middleware is been called right before the http request is made.
        this.interceptors.request.use(param => {
            return {
                defaults: {
                    headers: {
                        "authorization": `bearer ${this.gettoken()}`

      this.login = this.login.bind(this);
      this.getsome = this.getsome.bind(this);

   login (credentials) {
      return"/end-point", {...credentials})
      .then(response => this.settoken(

   getsome () {
      return this.get("/end-point")

i mean the implementation of the middleware depends on you, or if you prefer to create your own axios-es6-class it is the medium post where it came from


there are a lot of good solution but i use this

let token=localstorage.getitem("token");

var myaxios=axios.create({
  baseurl: 'https://localhost:5001',
  timeout: 700,
  headers: {'authorization': `bearer ${token}`}

export default myaxios;

then i import myaxios to my file and



// usetoken is hook i mad it

export const usetoken = () => {
     return json.parse(localstorage.getitem('user')).token || ''
const token = usetoken();

const axiosintance = axios.create({
    baseurl: api,
    headers: {
        'authorization':`bearer ${token}`

axiosintance.interceptors.request.use((req) => {
        req.headers.authorization = `bearer ${token}`;
    return req;


i use a separate file to init axios instance and at the same time, i add intercepters to it. then in each call, the intercepter will add the token to the request header for me.

import axios from 'axios';
import { gettoken } from '../hooks/usetoken';

const axiosinstance = axios.create({
  baseurl: process.env.react_app_base_url,

  (config) => {
    const token = gettoken();
    const auth = token ? `bearer ${token}` : '';
    config.headers.common['authorization'] = auth;
    return config;
  (error) => promise.reject(error),

export default axiosinstance;

here is how i use it in the service file.

import { canceltoken } from 'axios';
import { toolresponse } from '../types/tool';
import axiosinstance from './axios';

export const gettools = (canceltoken: canceltoken): promise<toolresponse> => {
  return axiosinstance.get('tool', { canceltoken });


just in case someone faced the same issue.

the issue here is when passing the header without data, the header's configuration will be in the payload data, so i needed to pass null instead of data then set the header's configuration.

const config = {
         headers: {
             "content-type": "application/json",
              "authorization": `bearer ${cookies.get("jwt")}`,
axios.get(`${base_url}`, null, config)


this works and i need to set the token only once in my app.js:

axios.defaults.headers.common = {
    'authorization': 'bearer ' + token

then i can make requests in my components without setting the header again.

"axios": "^0.19.0",


if you want to some data after passing token in header so that try this code

const api = 'your api'; 
const token = json.parse(sessionstorage.getitem('data'));
const token =; /*take only token and save in token variable*/
axios.get(api , { headers: {"authorization" : `bearer ${token}`} })
.then(res => {
.catch((error) => {


by using axios interceptor:

const service = axios.create({
  timeout: 20000 // request timeout

// request interceptor

  config => {
    // do something before request is sent

    config.headers["authorization"] = "bearer " + gettoken();
    return config;
  error => {


the second parameter of is data (not config). config is the third parameter. please see this for details:


you can create config once and use it everywhere.

const instance = axios.create({
  baseurl: '',
  timeout: 1000,
  headers: {'authorization': 'bearer '+token}

.then(response => {


here is a unique way of setting authorization token in axios. setting configuration to every axios call is not a good idea and you can change the default authorization token by:

import axios from 'axios';
axios.defaults.baseurl = 'http://localhost:1010/'
axios.defaults.headers.common = {'authorization': `bearer ${token}`}
export default axios;

some api require bearer to be written as bearer, so you can do:

axios.defaults.headers.common = {'authorization': `bearer ${token}`}

now you don't need to set configuration to every api call. now authorization token is set to every axios call.


const config = {
    headers: { authorization: `bearer ${token}` }

const bodyparameters = {
   key: "value"

the first parameter is the url.
the second is the json body that will be sent along your request.
the third parameter are the headers (among other things). which is json as well.

Related Query

More Query from same tag