I am new to angular (and programming), here is a seemingly simple question but I could not figure it out.
some tutorials suggests using $httpProvider.interceptors.push('interceptorName')
to manipulate the http request and response.
I want to know more about the interceptor thing so I look at the official document, but I could not find anything related to interceptor, there are only a method (useApplyAsync([value]);) and a property (defaults) in $httpProvider
(docs).
I know from other tutorials that an interceptor is a regular service factory and I know how to use it, but my question is: since the syntax is $httpProvider.interceptors.push('interceptorName')
, then I expect I will find a property called "interceptors" in $httpProvider
, but in fact I can't. Is something I miss to get this confusion? or is my concept totally wrong from the bottom?
HTTP Interceptors are used for adding custom logic for authentication, authorization, session/state management, logging, modifying Response, URL rewriting, Error handling, Caching, adding custom header, timestamp in the request /response, encrypt and decrypt the request and response information or manipulate the ...
To add or overwrite these defaults, simply add or remove a property from these configuration objects. To add headers for an HTTP method other than POST or PUT, simply add a new object with the lowercased HTTP method name as the key, e.g. $httpProvider. defaults.
get request Method Syntax: $http. get(url, { params: { params1: values1, params2:values2, params3:values3...... } });
The problem is as follows, $http. get is asynchronous, before the response is fetched, the function returns. Therefore the calling function gets the data as empty string.
Interceptors are in the documentation here.
Here's an example of how to write one.
.config([
'$httpProvider',
function($httpProvider) {
var interceptor = [
'$q',
'$rootScope',
'userSession',
function($q, $rootScope, userSession) {
var service = {
// run this function before making requests
'request': function(config) {
if (config.method === 'GET' || userSession.isAuth()) {
// the request looks good, so return the config
return config;
}
// bad request, so reject
return $q.reject(config);
}
};
return service;
}
];
$httpProvider.interceptors.push(interceptor);
}
])
The reason there's nothing on the $httpProvider
documentation page about interceptors is because the developers didn't include the following code in the $http
script from which the docs are generated:
/**
* @ngdoc property
* @name $httpProvider#interceptors
* @description
// etc
Documentation in general is known to be incomplete, inaccurate, and/or confusing. Until recently, I always thought I was the problem when I couldn't find or understand something, but I've found that it's often because documentation is just lousy. However, we should all be thankful that we have such great tools to use and keep in mind that perhaps the documentation is poor because time had to be focused on writing the tool instead of the manual for the tool.
The most reliable "documentation" is the source code itself, though it can be a lot less friendly to read! In the source code I linked above, you can see this.interceptors = []
. this
refers to the $httpProvider
, so it is assigning the property interceptors
to $httpProvider
with the value being an empty array. To add your interceptors, you simply push()
your interceptor to this array.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With