Search code examples
angularjshttpangular-httpangular-http-interceptors

Disable $httpProvider.interceptors.push for a specific url


I have a problem with finding a solution about my $httpProvider in AngularJS. I have a Accessservice which i handle response Error from server , this service can handle 401,404 .. http errors and make a redirection to a page .

But i have a specific url which i won't to apply this AccessService . How i can handle this ?

Config.js :

$httpProvider.interceptors.push('MyService');

MyServices:

angular.module('myapp').factory('MyService', function ($q, $location) {
return {
    responseError: function (rejection) {
        var code = rejection.status;

        if (code === 401) 
            $location.url('/auth');

        if (code === 403) 
            $location.url('/unauthorized');

        return $q.reject(rejection);
    }
};
});

in My Controller :

$scope.sendForm = function(){

    CustomService.posting($scope.form).then(function(data){
        if(data.status == 200){
                // code when 200
                // no problem here
        }

        if(data.status == 415)
            // line of code
        if(data.status == 401)
            // line of code

    });
};

There's a way to disable this interceptors for a specific controller or a http request.?

Thanks in Advance !


Solution

  • This is surely possible, you just need to wrap your if's in more conditional logic.

    if (rejection.config.url.match(/*regextomatchroute*/) {
        //handle the match
    } else {
        // your current ifs
    }