Sometimes might need to modify HTTP requests and responses. This could is for a variety of reasons such as adding global logic handling for HTTP errors. With interceptors, you can easily accomplish the this in your Angular applications.
varInterceptor =function($q, $location) {return{request:function(config) {console.log (config); returnconfig; }, Response:function(Result) {Console.log (' Repos: '); Result.data.splice (0). ForEach (function(Repo) {console.log (repo.name); }) returnresult; }, Responseerror:function(rejection) {Console.log (' Failed with ', rejection.status, ' status '); if(Rejection.status = = 403{$location. URL ('/login '); } return$q. Reject (rejection); }}};angular.module (' App ', []). config (function($httpProvider) {$httpProvider. Interceptors.push (Interceptor); }). Run (function($http) {$http. Get (' HTTPS://API.GITHUB.COM/USERS/BCLINKINBEARD/REPOSEFW '); });
In a lot of cases, interceptor can is used for Auth.
[AngularJS] Using AngularJS interceptors with $http