Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I TransformRequest GET parameters with Angular resource?

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)

like image 360
Aaron J Lang Avatar asked Aug 31 '26 01:08

Aaron J Lang


1 Answers

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;
    }
  };
});
like image 77
Stephanie Avatar answered Sep 02 '26 17:09

Stephanie