angular.js icon indicating copy to clipboard operation
angular.js copied to clipboard

encodeUriSegment in resource encodes params, should be optional

Open fredrikbonander opened this issue 14 years ago • 56 comments

When having a $resource and sending a param to be used in the url, it would be nice if there was an option not to encode it. OOB it encodes (in this case the "path") param before it's match agains the url. For example

// In SomeResourceName factory:
$resouce('/:path',  { path: 'default.json' }, ...)
// Useing SomeResourceName
SomeResourceName.get({ path: 'game/mygame.json' })

This will result in a call to url "/game%2Fmygame.json" instead of "/game/mygame.json".

There's a quick-fix-workaround:

// In angular-resource.js and method encodeUriSegment
  function encodeUriSegment(val) {
    return encodeUriQuery(val, true).
      replace(/%26/gi, '&').
      replace(/%3D/gi, '=').
      replace(/%2B/gi, '+'). 
      replace(/%2F/gi, '/'); // <--- Add this line
  }

I have no idea what will break, but as of know it works for me. One could also hijack the actions argument in ResourceFactory and pass a skip encode flag to the Route constructor's defaults argument to tell it to skip the encoding.

fredrikbonander avatar Sep 19 '12 11:09 fredrikbonander

+1

jkremser avatar Aug 20 '15 13:08 jkremser

+1

pavolloffay avatar Aug 21 '15 14:08 pavolloffay

You can use the same syntaxe like in the ui-router lib : http://angular-ui.github.io/ui-router/site/#/api/ui.router.util.type:UrlMatcher

patou avatar Sep 06 '15 09:09 patou

+1

it also happens in location

jiananshi avatar Oct 15 '15 18:10 jiananshi

:+1:

DaAwesomeP avatar Dec 20 '15 21:12 DaAwesomeP

+1

thesameson avatar Dec 24 '15 17:12 thesameson

I'm current doing a decode in order to send my original url to backend

.factory('decodeUriSegment', () => {
    return (url) => {
      return url.replace(/@/g, '%40')
        .replace(/:/g, '%3A')
        .replace(/\$/g, '%24')
        .replace(/,/g, '%2C')
        .replace(/\+/g, '%20');
    };
  });

jiananshi avatar Dec 24 '15 18:12 jiananshi

+1

tekstrand avatar Jan 22 '16 02:01 tekstrand

+1

kishorekumaru avatar Mar 04 '16 01:03 kishorekumaru

app.config(function($resourceProvider) {
    $resourceProvider.defaults.stripTrailingSlashes = false;
});

https://github.com/angular/angular.js/pull/5560

zapolnoch avatar Jul 05 '16 18:07 zapolnoch

+1

hadirsa avatar Aug 12 '16 17:08 hadirsa

i'm getting this error in angularjs $resource service Error: encodeUriSegment is not a function

ibrahim89 avatar Oct 21 '16 19:10 ibrahim89

@ibrahim89, make sure you use the same version of angular and angular-resource.

gkalpak avatar Oct 22 '16 05:10 gkalpak

@gkalpak, thank you !!

skarensmoll avatar Dec 09 '16 19:12 skarensmoll

my error is solved

ibrahim89 avatar Dec 10 '16 03:12 ibrahim89