0

I'm trying to properly encode query params on angular with the code below:

getAccount = function (accountEmail, accountCreationDate) {

      var data = {
        accountEmail: accountEmail,
        accountCreationDate: accountCreationDate
      };

      return $http.get('/administration/account.json', {params: $filter('noBlankValues')(data)}).then(
        function (result) {
          $log.debug('getAccount result: ' + JSON.stringify(result.data));
          return result.data.result;
        }
      );
    };

accountCreationDate is an ISO-8601 string (e.g. "2014-03-20T14:56:01.691+01:00"). According to http://docs.angularjs.org/api/ng/service/$http I'm passing params as an object, but in the frame I've the following "strange" query output:

?accountCreationDate=2014-03-20T14:56:01.691%2B01:00&[email protected]

i.e. the date '+' was encoded but not the rest of the object. Do you have any idea of what is going wrong and how to fix that?

PS: I know I can write manually the encoded query string, but I'm looking a more user-friendly solution.

2
  • By output you mean the url used to make the request, right? What output do you expect? Commented Apr 11, 2014 at 9:53
  • Right, I'm expecting that: ?accountCreationDate=2014-03-20T14%3A56%3A01.691%2B01%3A00&accountEmail=test%40test.com Commented Apr 12, 2014 at 10:43

1 Answer 1

1

Ok, that was solved by using the following transform function:

var transform = function () {
      headers: {
        'Content-type': 'application/x-www-form-urlencoded;charset=UTF-8'
      },
      transformRequest: function (obj) {
        var str = [];
        for (var p in obj) {
          if ((typeof obj[p] !== 'undefined') &&
            (typeof obj[p] !== 'function')) {

            if (obj[p] instanceof Date) {
              str.push(encodeURIComponent(p) + '=' + encodeURIComponent(obj[p].toISOString()));
            }
            else if (obj[p] instanceof Array) {
              for (var i in obj[p]) {
                if (obj[p][i] instanceof Object) {
                  str.push(encodeURIComponent(p) + '=' + encodeURIComponent(JSON.stringify(obj[p][i])));
                } else {
                  str.push(encodeURIComponent(p) + '=' + encodeURIComponent(obj[p][i]));
                }
              }
            }
            else if (obj[p] instanceof Object) {
              str.push(encodeURIComponent(p) + '=' + encodeURIComponent(JSON.stringify(obj[p])));
            }
            else {
              str.push(encodeURIComponent(p) + '=' + encodeURIComponent(obj[p]));
            }
          }
        }
        return str.join('&');
      }
    }
};
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.