1

I am using angular 1.4.8 and there is bug i thing with encode parameter in get request.

This is my service :

   angular
        .module('test')
        .factory('User',User);

    /* @ngInject */
    function User($resource) {
        return $resource('api/users/:login', {}, {
            'query': {method: 'GET', isArray: false},
            'get': {
                method: 'GET',
                transformResponse: function (data) {
                    data = angular.fromJson(data);
                    return data;
                }
            },
            'save': {method: 'POST'},
            'update': {method: 'PUT'},
            'delete': {method: 'DELETE'}
        });
    }

and this when i use it:

     return User.get({
                        page: 1,
                        size: 20,
                        sort: 'email asc'  //THIS IS A PROBLEM
                    }
                    ).$promise.then(function (data, headersGetter) {

                       return data.content;
                    });

My request is :

http://localhost:3000/api/users?page=0&size=10&sort=email+asc

But its should look like :

http://localhost:3000/api/users?page=0&size=10&sort=email%20asc

Why angular not properly encode that parameter ?? How to fix this issue ?

luckybastard
  • 157
  • 1
  • 3
  • 12
  • 1
    GET parameters are not designed to work with spaces. I would use two fields: sortField and sortType – Serginho Jan 19 '16 at 14:18
  • Try adding `transformRequest: []` to the 'get' definition. This should make sure no unwanted transformations are happening to the request. – jim0thy Jan 19 '16 at 14:39
  • can't you just decode the param at server? – charlietfl Jan 19 '16 at 14:42
  • I dont manage server so i can't change implementation – luckybastard Jan 19 '16 at 15:19
  • @jim0thy i add as yout say : ```'get': { method: 'GET', transformRequest: [], transformResponse: function (data) { data = angular.fromJson(data); return data; } },``` its still not working i have got + sign – luckybastard Jan 19 '16 at 15:23

1 Answers1

1

You can manually encode if needed. Try this:

sort: encodeURIComponent('email asc')

Or better yet, wrap the entire finished url string in encodeURIComponent.

Note that spaces in url parameters are not valid. Encoding will add %2520 where the space was.

See this question asking/explaining reason for %2520 instead of %20

See this other question and the answer quoting the RFC.

Community
  • 1
  • 1
Daniel Nalbach
  • 1,033
  • 11
  • 17