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)

2
  • $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? Commented May 9, 2014 at 10:59
  • There is also an actions.interceptor options, but it only accepts response interceptors, not request interceptors. Commented May 9, 2014 at 11:18

1 Answer 1

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;
    }
  };
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.