Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

encodeUriSegment in resource encodes params, should be optional #1388

Open

Description

@fredrikbonander

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.

Activity

  1. fredrikbonander commented on Sep 19, 2012

    @fredrikbonander
    Author

    Created a gist with the changes for hijacking :) https://gist.github.com/3749345

  2. manuelsantillan commented on Oct 29, 2012

    @manuelsantillan

    +1 to this. Provides greater flexibility. In our use case, we are using spring data rest for the backend and search methods are path params that we need to map to actions, ideally in the same resource object as for the basic CRUD ops.

  3. georgiosd commented on Nov 22, 2012

    @georgiosd

    +1. Our ids contain slashes (we use RavenDB) and it would be a clean solution if it wasn't encoded

  4. saden1 commented on Apr 1, 2013

    @saden1

    Making it optional is fine but please insure that it's enabled by default.

    It is ill advised to not encode URIs. Not doing so might seem "clean" now but I assure you it will bite you in the ass in this life or the next.

  5. toddb commented on Apr 26, 2013

    @toddb

    If I read http://www.ietf.org/rfc/rfc3986.txt correctly, we should be allowed the unencoded slash for the fragment.

    3.5. Fragment

    The fragment identifier component of a URI allows indirect
    identification of a secondary resource by reference to a primary
    resource and additional identifying information. The identified
    secondary resource may be some portion or subset of the primary
    resource, some view on representations of the primary resource, or
    some other resource defined or described by those representations. A
    fragment identifier component is indicated by the presence of a
    number sign ("#") character and terminated by the end of the URI.

     fragment    = *( pchar / "/" / "?" )
    

    ...
    The characters slash ("/") and question mark ("?") are allowed to
    represent data within the fragment identifier. Beware that some
    older, erroneous implementations may not handle this data correctly
    when it is used as the base URI for relative references (Section
    5.1).

    Use case: $location.hash('/secondary-resource')

  6. ProLoser commented on May 10, 2013

    @ProLoser
    Contributor

    I'm just curious, is it possible to avoid this issue by double-encoding your params?

  7. toddb commented on May 11, 2013

    @toddb

    Sorry, it then makes it even worse. Just to confirm I tried out a number of opens even though it felt futile!

    '/', '%252F' --> %25252F

    Also,

    / %2f --> %252F
    '/', '//' --> %2F%2F

    Thanks for the thought. Request still stands.

  8. sephie commented on Jul 10, 2013

    @sephie

    Ran into this today. +1
    I might not like it, but our id's contain slashes as well. This needs to be optional.

  9. stevermeister commented on Jul 10, 2013

    @stevermeister
    Contributor

    +1

  10. chelexey commented on Sep 28, 2013

    @chelexey

    +1

  11. mrzepinski commented on Nov 21, 2013

    @mrzepinski

    +1

  12. e0ipso commented on Dec 3, 2013

    @e0ipso

    +1

  13. jchonde commented on Jan 28, 2014

    @jchonde

    +1

  14. self-assigned this
    on Feb 6, 2014
  15. 39 remaining items

  16. pavolloffay commented on Aug 21, 2015

    @pavolloffay

    +1

  17. patou commented on Sep 6, 2015

    @patou

    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

  18. jiananshi commented on Oct 15, 2015

    @jiananshi

    +1

    it also happens in location

  19. DaAwesomeP commented on Dec 20, 2015

    @DaAwesomeP

    👍

  20. thesameson commented on Dec 24, 2015

    @thesameson

    +1

  21. jiananshi commented on Dec 24, 2015

    @jiananshi

    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');
        };
      });
  22. tekstrand commented on Jan 22, 2016

    @tekstrand

    +1

  23. kishorekumaru commented on Mar 4, 2016

    @kishorekumaru

    +1

  24. zapolnoch commented on Jul 5, 2016

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

    #5560

  25. hadirsa commented on Aug 12, 2016

    @hadirsa

    +1

  26. ibrahim89 commented on Oct 21, 2016

    @ibrahim89

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

  27. gkalpak commented on Oct 22, 2016

    @gkalpak
    Member

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

  28. skarensmoll commented on Dec 9, 2016

    @skarensmoll

    @gkalpak, thank you !!

  29. ibrahim89 commented on Dec 10, 2016

    @ibrahim89

    my error is solved

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions