Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
AngularJS interceptor code which preprocesses the http response sent from a RESTful Web API
'use strict';
angular.module('appinterceptors').factory('WebAPIInterceptor', WebAPIInterceptor);
WebAPIInterceptor.$inject = ['$q'];
function WebAPIInterceptor($q) {
return {
request: request,
requestError: requestError,
response: response,
responseError: responseError
//request success
function request(config) {
// Return the config or promise.
return config || $q.when(config);
//request error
function requestError(rejection) {
// Return the promise rejection.
return $q.reject(rejection);
// response success
function response(response) {
//checking whether we got our AjaxModel
if ("Success") &&"Message") &&"Model")) {
if ( === false) {
//as needed show error message to the user
//reject the response
return $q.reject(response);
else { =;
// Return the response or promise.
return response || $q.when(response);
//response Error
function responseError(rejection) {
// Return the promise rejection.
return $q.reject(rejection);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment