How to add an add request parameter to each Angular.js $ http request (for example, to start an xdebug session)

My hybrid application is based on AngularJS and uses php REST api.

I would like to debug php api directly from my Angular app instead, to use a REST or Postman console. This will save a lot of time, especially for POST and PUT requests.

To do this, I will need to add a parameter to each request like this:

http://localhost:8000/api/contacts?XDEBUG_SESSION_START=PHPSTORM 

Can I set up $ http for this?

+4
source share
1 answer

You can use httpInterceptor for this (the official $ http documentation contains additional information)

 // register the interceptor as a service $provide.factory('xdebugInterceptor', function($q) { return { // optional method 'request': function(config) { // do something on success // !!! adjust the config object // add request param XDEBUG_SESSION_START=PHPSTORM // it will be added to every made request config.params = config.params || {}; config.params.XDEBUG_SESSION_START: "PHPSTORM"; return config; }, // optional method 'requestError': function(rejection) { // do something on error return $q.reject(rejection); }, // optional method 'response': function(response) { // do something on success return response; }, // optional method 'responseError': function(rejection) { // do something on error return $q.reject(rejection); } }; }); // make this conditional so you use it only in DEV mode $httpProvider.interceptors.push('xdebugInterceptor'); 
+10
source

All Articles