SlideShare a Scribd company logo
AngularJS $http Interceptors
Explanation and Examples
Brian Swartzfager
bcswartz@gmail.com
http://thoughtdelimited.org
What are $http Interceptors
• They are event functions triggered during the
lifecycle of an HTTP call made via AngularJS.
• They will run anytime you execute:
– $http
– $resource
– $httpBackend
Types of Interceptors
• request ( configuration )
– Receives and returns a configuration object that defines the outgoing
HTTP call
• requestError ( rejection )
– Receives a rejection object when a previous interceptor (like a request
interceptor) returns an error or rejection
• response ( response )
– Receives and returns a response object from a successful HTTP call
• responseError ( rejection )
– Receives a rejection object when a previous response interceptor
returns an error or rejection
Documentation:
https://docs.angularjs.org/api/ng/service/$http#interceptors
Created as Service Factory Functions
Added to the $httpProvider
Interceptors Array
Uses For Interceptors
• Useful anytime you need to apply the same
checks/actions on HTTP requests and
responses throughout your application
(“cross-cutting concerns”)
– Configuring the outgoing requests in a certain way
– Altering the incoming response for easier
consumption
– Handling certain response errors
– Checking authentication status
Conditional Interceptor Behavior
• Use the properties and functions of an injected
object to control or suppress interceptor
behavior.
– An object in the $rootScope
– Or an Angular service
request Example
responseError Example
responseError: 400 error
responseError: 403 error
responseError: 500 server error
response Example
Better approach:
• Move the location management logic to a service function and
inject that service into this interceptor and the responseError
interceptor.
Questions?

More Related Content

PPT
Postman.ppt
PDF
Angular Dependency Injection
PPT
Introduction to the Web API
PPTX
REST API Design & Development
ODP
Angular 4 The new Http Client Module
PPTX
RESTful API - Best Practices
PPTX
Selenium locators: ID, Name, xpath, CSS Selector advance methods
PPTX
Intro to React
Postman.ppt
Angular Dependency Injection
Introduction to the Web API
REST API Design & Development
Angular 4 The new Http Client Module
RESTful API - Best Practices
Selenium locators: ID, Name, xpath, CSS Selector advance methods
Intro to React

What's hot (20)

PPTX
Angular 2.0 Dependency injection
PDF
API_Testing_with_Postman
PDF
REST API and CRUD
PDF
Angular - Chapter 3 - Components
PDF
Selenium IDE LOCATORS
PPTX
PPSX
Rest api standards and best practices
PPTX
Servlets
PPTX
reactJS
PDF
Angular Directives
PPTX
Selenium-Locators
PPTX
React + Redux Introduction
PPTX
Angularjs PPT
PPTX
Automation - web testing with selenium
PDF
Asynchronous JavaScript Programming with Callbacks & Promises
PDF
An Introduction To Automated API Testing
PPTX
ReactJS presentation.pptx
PDF
ES6 presentation
PPSX
API Test Automation
PPTX
Spring data jpa
Angular 2.0 Dependency injection
API_Testing_with_Postman
REST API and CRUD
Angular - Chapter 3 - Components
Selenium IDE LOCATORS
Rest api standards and best practices
Servlets
reactJS
Angular Directives
Selenium-Locators
React + Redux Introduction
Angularjs PPT
Automation - web testing with selenium
Asynchronous JavaScript Programming with Callbacks & Promises
An Introduction To Automated API Testing
ReactJS presentation.pptx
ES6 presentation
API Test Automation
Spring data jpa
Ad

Viewers also liked (12)

PDF
Keeping Things Simple In A Growing AngularJS App.
PPTX
AngularJS Best Practices
PDF
HTTP Interceptors com AngularJS
PPTX
AngularJS Architecture
PDF
AngularJS performance & production tips
PDF
AngularJS 101 - Everything you need to know to get started
PDF
AngularJS application architecture
PPTX
Introduction to Angularjs
PDF
29 Essential AngularJS Interview Questions
PDF
Building Enterprise Applications with AngularJS (GDG DevFest Karlsruhe 2014)
PDF
Zukunftssichere Anwendungen mit AngularJS 1.x entwickeln (GDG DevFest Karlsru...
PPTX
Preeclámpsia -Toxemia Gravídica-Elza Herminia Sabino Mendes e Thiago Veríssimo
Keeping Things Simple In A Growing AngularJS App.
AngularJS Best Practices
HTTP Interceptors com AngularJS
AngularJS Architecture
AngularJS performance & production tips
AngularJS 101 - Everything you need to know to get started
AngularJS application architecture
Introduction to Angularjs
29 Essential AngularJS Interview Questions
Building Enterprise Applications with AngularJS (GDG DevFest Karlsruhe 2014)
Zukunftssichere Anwendungen mit AngularJS 1.x entwickeln (GDG DevFest Karlsru...
Preeclámpsia -Toxemia Gravídica-Elza Herminia Sabino Mendes e Thiago Veríssimo
Ad

Recently uploaded (20)

PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Spectral efficient network and resource selection model in 5G networks
PPTX
Cloud computing and distributed systems.
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Machine learning based COVID-19 study performance prediction
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
NewMind AI Monthly Chronicles - July 2025
Review of recent advances in non-invasive hemoglobin estimation
Spectral efficient network and resource selection model in 5G networks
Cloud computing and distributed systems.
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
“AI and Expert System Decision Support & Business Intelligence Systems”
Encapsulation_ Review paper, used for researhc scholars
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Chapter 3 Spatial Domain Image Processing.pdf
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
MYSQL Presentation for SQL database connectivity
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Per capita expenditure prediction using model stacking based on satellite ima...
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Diabetes mellitus diagnosis method based random forest with bat algorithm
Machine learning based COVID-19 study performance prediction
Network Security Unit 5.pdf for BCA BBA.
Unlocking AI with Model Context Protocol (MCP)
NewMind AI Monthly Chronicles - July 2025

AngularJS $http Interceptors (Explanation and Examples)

  • 1. AngularJS $http Interceptors Explanation and Examples Brian Swartzfager bcswartz@gmail.com http://thoughtdelimited.org
  • 2. What are $http Interceptors • They are event functions triggered during the lifecycle of an HTTP call made via AngularJS. • They will run anytime you execute: – $http – $resource – $httpBackend
  • 3. Types of Interceptors • request ( configuration ) – Receives and returns a configuration object that defines the outgoing HTTP call • requestError ( rejection ) – Receives a rejection object when a previous interceptor (like a request interceptor) returns an error or rejection • response ( response ) – Receives and returns a response object from a successful HTTP call • responseError ( rejection ) – Receives a rejection object when a previous response interceptor returns an error or rejection Documentation: https://docs.angularjs.org/api/ng/service/$http#interceptors
  • 4. Created as Service Factory Functions
  • 5. Added to the $httpProvider Interceptors Array
  • 6. Uses For Interceptors • Useful anytime you need to apply the same checks/actions on HTTP requests and responses throughout your application (“cross-cutting concerns”) – Configuring the outgoing requests in a certain way – Altering the incoming response for easier consumption – Handling certain response errors – Checking authentication status
  • 7. Conditional Interceptor Behavior • Use the properties and functions of an injected object to control or suppress interceptor behavior. – An object in the $rootScope – Or an Angular service
  • 13. response Example Better approach: • Move the location management logic to a service function and inject that service into this interceptor and the responseError interceptor.