3

When using ngResource/$resource, it is possible to implement custom (de)serialisation by specifying transformRequest/transformResponse. However these only control the request's body (data), so how can one manipulate the query parameters in a GET request?

Specifically, I would like to json-encode all the parameter values.

Simple case:
?user=123 is user with id 123
?user="123" is user with name 123

Complex case:
Passing objects/hashes in a GET request. For example using a mongo-like syntax to specify request criteria/projection. (Please note this question is not about mongo specifcally)

Aaron J Lang
  • 2,048
  • 3
  • 20
  • 27
  • $resource takes an actions.params option which can be a hash of functions against parameter names. Are these functions passed anything useful? Anyway for them to get hold of the myResource state or default param string? – Aaron J Lang May 09 '14 at 10:59
  • There is also an actions.interceptor options, but it only accepts response interceptors, not request interceptors. – Aaron J Lang May 09 '14 at 11:18

1 Answers1

3

You can use a request interceptor for this:

$httpProvider.interceptors.push(function() {
  return {
   'request': function(config) {
       //config.params contains query/request parameters
       if (config.params){
         //Do something here...
       }
       return config;
    }
  };
});
Stephanie
  • 508
  • 5
  • 14