Axios - Remove headers Authorization in 1 call only
Asked Answered
F

11

50

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

Foredate answered 9/10, 2017 at 23:29 Comment(0)
E
71

Try the following

delete axios.defaults.headers.common["Authorization"];
// or which ever header you have to remove
Epidemic answered 10/10, 2017 at 7:34 Comment(8)
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 oneForedate
Can you try this transformRequest: [(data, headers) => { delete headers.common.Authorization return data }]Epidemic
I am referring to the following issue on the github library -> axios/issueEpidemic
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. lolForedate
So this will keep the token for the other calls right?Foredate
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.Epidemic
I hope my answers were of any helpEpidemic
Yes it will keep the tokens for other calls intact.!Epidemic
B
52

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 :)

Boston answered 4/7, 2020 at 3:24 Comment(3)
You can also simply delete headers.common in the callback if you wish to remove all default headersDown
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.Nap
Thanks brianf - I’ve now updated my answer accordingly.Boston
C
17

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");

Coray answered 24/5, 2020 at 15:13 Comment(1)
This one works for me while transformRequest doesn't works.Taeniacide
C
8

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.

Conchita answered 19/5, 2020 at 12:55 Comment(1)
Yeah but if you work with a service like aws this can be a problemAgan
B
5

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

Bregenz answered 4/4, 2018 at 12:27 Comment(0)
D
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;
    }
});
Down answered 18/11, 2020 at 12:21 Comment(0)
L
3
delete request.defaults.headers.common.Authorization

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

Lilliamlillian answered 11/7, 2021 at 8:41 Comment(0)
A
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.

Accidence answered 16/12, 2021 at 8:14 Comment(0)
A
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.

Augie answered 20/1, 2023 at 7:21 Comment(1)
Correct me if I'm wrong, but with the latest axios, it's only delete headers['Authorization'];Coulometer
H
-1
const mynewinstance = axios.create();
mynewinstance.defaults.headers.common = {};
const output = await mynewinstance.get(`https://google.com`);
Herstein answered 20/12, 2022 at 19:59 Comment(1)
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?Overprize
L
-3

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

Limewater answered 1/11, 2021 at 8:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.