Angular -resource.js encodeUriSegment Issue

I have a problem with a request for backup resources when the uri resource has several subfolders:

Example:

.factory('SomeFactory', function ($resource) {
return $resource('/path/subPath/otherSubPath/:id', {}, {
show: { method: 'GET', params: { id: '@id' } }
})
}) ;

When I call SomeFactory.show in the controller, I get an error the server responded with status 400 (Bad Request) This is because the browser is looking for uri:

http://server/path/subPath%2FotherSubPat/id

Note that% 2F replaces / (slash) in uri, I tried many tricks in javascript to make this work; But the only solution was to add the following replacement for the last line (/% 2F / gi, '/'); in the angular -resource.js encodeUriSegment method.

Please tell me if this approach is correct.

function encodeUriSegment(val) {
  return encodeUriQuery(val, true).
    replace(/%26/gi, '&').
    replace(/%3D/gi, '=').
    replace(/%2B/gi, '+').
    replace(/%2F/gi, '/');
}

Thanks.

+4
source share
3 answers

, , github:

https://github.com/angular/angular.js/issues/1388#issue-6979382

, . , , , ( ), angular-resource.js - lib, .

, uri Angular .

+1

http-:

module.constant("interceptULRS",
  [new RegExp('.+/path/.*')]);

module.config(['$httpProvider', 'interceptULRS', function($httpProvider, interceptULRS) {
var ENCODED_SLASH = new RegExp("%2F", 'g');
$httpProvider.interceptors.push(function ($q) {
  return {
    'request': function (config) {
      var url = config.url;

      for (var i = 0; i < interceptULRS.length; i++) {
        var regex = interceptULRS[i];
        if (url.match(regex)) {
          url = url.replace(ENCODED_SLASH, "/");

          // end there is only one matching url
          break;
        }
      }

      config.url = url;
      return config || $q.when(config);
    }
  };
});
}]);

, URL-.

+1

@Pavol ,

var ENCODED_SLASH = new RegExp("%2F", 'g');
var request = function (config) {
    var matches = config.url.match(ENCODED_SLASH);
    if (matches && matches.length) {
        config.url = config.url.replace(ENCODED_SLASH, "/");
    }
    return config || $q.when(config);
};

ENCODED_SLASH , , "% 2F" URL-.

As I work more and more with angular -resource, I find the use and understanding of $ httpProvider.interceptors more important.

0
source

All Articles