最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - VueJs and VueResource, removing header fields from a Ajax request - Stack Overflow

programmeradmin1浏览0评论

When I instantiate a Vuejs (2.2.6) and Vue-resource (1.2.1), I set the header authorization with the following code, this way I can authorize all requests to my API:

Vue.http.headersmon.AUTHORIZATION = 'BEARER ...';

However, I want to make a request for a third party API, and I do not want the Authorization field to be sent. Additionally, this API does not allow you to use this authorization header.

let CEP = '';

this.$http.get('/' + CEP + '/json')
    .then(response => {
        console.log(response.headers);
    });

This way the authorization field is sent with the header, on Access-Control-Request-Headers:

I tried to remove some header fields with the following codes, without success.

this.$http.headersmon.AUTHORIZATION = null;
this.$http.headersmon['Access-Control-Allow-Headers'] = null;

this.$http.get('/' + CEP + '/json')
    .then(response => {
        console.log(response.headers);
    });

In the vue-resource documentation, there is the possibility of inserting an object to force the request configuration, but the documentation isn't plete.

this.$http.get('/' + CEP + '/json', {
   ...here...
}).then(response => {
    console.log(response.headers);
});

Is there any way to remove the Authorization field, or any other field from a given request?
Thanks.

* UPDATED *

By using the interceptors (as in the below sample) I can edit the request but I can not delete a particular field.

Vue.http.interceptors.push((request, next) => {

    const viacep = request.url.includes('viacep.br');

    if (viacep) {
        request.headers.set('AUTHORIZATION', 'TRY THIS');
    }

    next(response => {});
});

Try to Delete:

Vue.http.interceptors.push((request, next) => {

    const viacep = request.url.includes('viacep.br');

    if (viacep) {
        request.headers.delete('AUTHORIZATION');
    }

    next(response => {});
});

When I instantiate a Vuejs (2.2.6) and Vue-resource (1.2.1), I set the header authorization with the following code, this way I can authorize all requests to my API:

Vue.http.headers.mon.AUTHORIZATION = 'BEARER ...';

However, I want to make a request for a third party API, and I do not want the Authorization field to be sent. Additionally, this API does not allow you to use this authorization header.

let CEP = '';

this.$http.get('https://viacep..br/ws/' + CEP + '/json')
    .then(response => {
        console.log(response.headers);
    });

This way the authorization field is sent with the header, on Access-Control-Request-Headers:

I tried to remove some header fields with the following codes, without success.

this.$http.headers.mon.AUTHORIZATION = null;
this.$http.headers.mon['Access-Control-Allow-Headers'] = null;

this.$http.get('https://viacep..br/ws/' + CEP + '/json')
    .then(response => {
        console.log(response.headers);
    });

In the vue-resource documentation, there is the possibility of inserting an object to force the request configuration, but the documentation isn't plete.

this.$http.get('https://viacep..br/ws/' + CEP + '/json', {
   ...here...
}).then(response => {
    console.log(response.headers);
});

Is there any way to remove the Authorization field, or any other field from a given request?
Thanks.

* UPDATED *

By using the interceptors (as in the below sample) I can edit the request but I can not delete a particular field.

Vue.http.interceptors.push((request, next) => {

    const viacep = request.url.includes('viacep..br');

    if (viacep) {
        request.headers.set('AUTHORIZATION', 'TRY THIS');
    }

    next(response => {});
});

Try to Delete:

Vue.http.interceptors.push((request, next) => {

    const viacep = request.url.includes('viacep..br');

    if (viacep) {
        request.headers.delete('AUTHORIZATION');
    }

    next(response => {});
});

Share Improve this question edited Apr 6, 2017 at 0:57 Thiago Pereira asked Apr 5, 2017 at 22:33 Thiago PereiraThiago Pereira 61414 silver badges25 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 10

Use an interceptor, inspect the request, and remove the header if needed.

Vue.http.interceptors.push(function(request, next) {

  const removeAuthHeaders = request.url.includes("viacep..br");

  if (removeAuthHeaders){
    request.headers.delete('Access-Control-Allow-Headers')
    request.headers.delete('AUTHORIZATION')
  }
  else {
    request.headers.set('Access-Control-Allow-Headers', <value>)
    request.headers.set('AUTHORIZATION', <value>)
  }
  next();
});
发布评论

评论列表(0)

  1. 暂无评论