45

How can I remove the axios.defaults.headers.common.Authorization only in 1 call?

I'm setting the default for all the calls to my domain but I have 1 call that I make on another domain and if the token is passed the call gives me an error, when there's no default Auth token saved everything works fine.

So what I'm trying to do is not pass the Auth in that specific call

I tried this but it doesn't work

    loadApiCoins({ commit }) {
        Vue.axios({
            method: 'get',
            url: 'https://api.coinmarketcap.com/v1/ticker/',
            headers: {
                'Authorization': '',
            },
        }).then(...)
    },

I also tried auth: {...} but that doesn't work either. What's the solution? Thanks

11 Answers11

65

Try the following

delete axios.defaults.headers.common["Authorization"];
// or which ever header you have to remove
Soviut
  • 88,194
  • 49
  • 192
  • 260
Apurva jain
  • 1,460
  • 13
  • 14
  • 2
    but wouldn't this remove the auth for all requests? I still need to use the token for other requests that will happen very shortly after this specific one –  Oct 10 '17 at 07:44
  • 18
    Can you try this `transformRequest: [(data, headers) => { delete headers.common.Authorization return data }]` – Apurva jain Oct 10 '17 at 07:58
  • 2
    I am referring to the following issue on the github library -> [axios/issue](https://github.com/axios/axios/issues/382) – Apurva jain Oct 10 '17 at 07:59
  • 3
    That does seem to work! Status: 200 has never looked so great. I'm mad that I spent a great deal of time going through Axios issues yesterday and didn't find that. lol –  Oct 10 '17 at 08:07
  • So this will keep the token for the other calls right? –  Oct 10 '17 at 08:09
  • Yes it should keep the token for other request. Make sure to use the `transform request` function only inside the request you want to modify the headers. – Apurva jain Oct 10 '17 at 08:34
  • I hope my answers were of any help – Apurva jain Oct 10 '17 at 08:36
  • Yes it will keep the tokens for other calls intact.! – Apurva jain Sep 02 '19 at 03:59
46

To send a request without:

  • Modifying global axios defaults
  • Creating a new axios instance

Change your request similarly to this:

axios.get('http://example.com', {transformRequest: (data, headers) => {
    delete headers.common['Authorization'];
    return data;
  }
});

The answer I was looking for was posted in the comments of Apurva jain's answer, but hasn't been made an individual answer, so I've posted it separately for easy reference :)

Jules
  • 1,677
  • 1
  • 19
  • 25
  • 1
    You can also simply delete `headers.common` in the callback if you wish to remove all default headers – Lou Garczynski Nov 18 '20 at 12:24
  • 1
    This answer uses a different syntax than Apurva jain's comment. `transformRequest: [(data, headers) => { delete headers.common.Authorization return data }]` is an array and returns the data parameter. `transformRequest: (data, headers) => { delete headers.common['Authorization']; }` is a function with no return value. The first format worked for me. – brianf May 14 '21 at 15:25
  • Thanks brianf - I’ve now updated my answer accordingly. – Jules May 15 '21 at 21:34
16

if you already have a default 'Authorization' for all requests you can create an instance for that specific request

var instance = axios.create();
delete instance.defaults.headers.common['Authorization'];

instance.get("http://api.com");

Bahi Hussein
  • 433
  • 5
  • 10
6

I got the same issue trying to query S3 with my web-service auth token. Fixed it with this.

axios.get("http://api.com", {
        headers:{ Authorization:""}
});

You can change the default headers to an empty string, this won't affect the common default headers. Though not entirely sure if all web services will ignore the empty string header.

Bob Kimani
  • 1,114
  • 1
  • 20
  • 38
5

delete axios.defaults.headers.common["Authorization"]; will solve the problem. But remember to add the authorization header back.

Pavindu
  • 2,684
  • 6
  • 44
  • 77
iamonuwa
  • 81
  • 3
  • 5
3

A simple solution is to remove all common header from a new axios instance:

const awsAxios = axios.create({
    transformRequest: (data, headers) => {
        // Remove all shared headers
        delete headers.common;
        // or just the auth header
        delete headers.common.Authorization;
    }
});
Lou Garczynski
  • 624
  • 3
  • 10
3
delete request.defaults.headers.common.Authorization

That request should be return of a $axios.create()

Saeed
  • 3,294
  • 5
  • 35
  • 52
1

To extend on @phantomraa's answer, you might want to use

this.$axios.$get(
      url, {
      // modify auth header for current request only
      transformRequest: (data, headers) => {
        // prevent the header from being added by default
        delete headers.common['Authorization'];
        // some libraries might set it directly as well, e.g. nuxtjs/auth
        delete headers['Authorization'];
        return data;
      }
})

Sorry, need a bit more rep to just comment.

Goatfryed
  • 118
  • 6
0

According to the latest axios Request Config documentation we can use transformRequest:

// This is only applicable for request methods 'PUT', 'POST', 'PATCH' and 'DELETE'

// The last function in the array must return a string or an instance of Buffer, ArrayBuffer,

// FormData or Stream

// You may modify the headers object.

An example:

axiosInstance.post('/api/auth-token', { email, password }, {
  transformRequest: [
    (data, headers) => {
      delete headers.common['Authorization'];
      return JSON.stringify(data);
    },
  ],
});

Please note the call to JSON.stringify as mentioned in the documentation, you need to return a Buffer, ArrayBuffer, FormData or Stream.

Robert T.
  • 1,620
  • 2
  • 13
  • 20
-1
const mynewinstance = axios.create();
mynewinstance.defaults.headers.common = {};
const output = await mynewinstance.get(`https://google.com`);
kingsec
  • 21
  • 1
  • 4
  • 2
    Remember that Stack Overflow isn't just intended to solve the immediate problem, but also to help future readers find solutions to similar problems, which requires understanding the underlying code. This is especially important for members of our community who are beginners, and not familiar with the syntax. Given that, **can you [edit] your answer to include an explanation of what you're doing** and why you believe it is the best approach? – Jeremy Caney Dec 21 '22 at 00:14
-3

delete axios.defaults.headers.common["language"];