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

I was using an HTTP interceptor to transform URLs. It was working until I tested on IE11. Angular breaks on XHR requests to URLs including % in IE11. Not sure about IE10. Angular is supposed to support IE9+ (reference)

Guess I'm going back to the modified ngResource..

connorbode avatar Jun 17 '14 19:06 connorbode

@connorbode --- dude, like it was mentioned in your issue, write a failing test case =) This path SHOULD already be covered, so there's two possibilities, either it's not covered and should be, or you're doing something to break this.

Lets find out which it is!

caitp avatar Jun 17 '14 19:06 caitp

+1

an-dev avatar Jun 24 '14 15:06 an-dev

+1

ksokol avatar Jul 28 '14 15:07 ksokol

+1

Impossible to work with CouchDB and design-documents (_design/cafehub/_view/menu_items)

rkakrik avatar Aug 22 '14 18:08 rkakrik

Can you guys +1 my pull request instead of this issue? It fixes the issue but has been generally ignored since its submission. https://github.com/angular/angular.js/pull/7940

connorbode avatar Aug 22 '14 18:08 connorbode

+1 Really need this to interact with some legacy webservices which do not perform URLdecode

ghost avatar Sep 23 '14 18:09 ghost

+1

There are definitely viable, useful circumstances where a user will want to disable the encoding of URL parameters (for instance, where you want to append to a base URL a path to a resource).

rossbayer avatar Oct 21 '14 21:10 rossbayer

+1

pachoulie avatar Feb 09 '15 23:02 pachoulie

+1

geoyws avatar Feb 19 '15 07:02 geoyws

+1

jcfontecha avatar Mar 20 '15 23:03 jcfontecha

+1

rashad803 avatar Mar 26 '15 00:03 rashad803

@toddb just FYI, your spec quote refers to the fragment part of a URL, i.e. the #foo/bar part, where escaping a slash is indeed not required or appropriate. But this bug is about escaping a slash in the main path part of the URL. In general, I don't think the RFC applies to this, how Angular's $resource service handles pattern matching and URL construction really doesn't concern the RFC much.

Not escaping slashes in a regular :param style parameter has the problem that it makes the pattern non-invertible. E.g. if you have /x/:param, and you allow generating /x/y/z for {param: 'y/z'}, the resulting /x/y/z will fail to parse against the URL pattern.

IMHO it might make sense to have a feature in URL patterns to accept a star pattern that eats slashes, e.g. "/:param1/:param2/*pathParam", where *pathParam would eat slashes in the URL. For * params, it'd then make sense to also accept slashes without escaping them.

mprobst avatar Apr 29 '15 17:04 mprobst

@mprobst - you are quite correct that I was logging a bug only around the fragment. The code as I use it also affects the fragment. I am not using the $resource service but rather $location. If my memory serves me there are a couple of implementations of the uriSegment code.

The patch by @connorbode from a quick read will address the issue. Cheers

toddb avatar Apr 29 '15 19:04 toddb

+1

adamhadani avatar Apr 29 '15 22:04 adamhadani

+1

nmehta6 avatar May 08 '15 18:05 nmehta6

+1

paulistoan avatar May 15 '15 09:05 paulistoan

+1

strokyl avatar Jul 07 '15 09:07 strokyl

+1

kinoakter avatar Aug 12 '15 11:08 kinoakter

+1

tonycapone avatar Aug 19 '15 01:08 tonycapone