-2

I would like to know how I can escape / in angularjs so that I can send it as a path variable to call a restful service. As of now I am getting 404 whenever I try to send a url with path variable values having /; even if I encode it doesn't work.

For example

http://monish.home.com/payment/9a2c1ae67d4ff85e561679fcff/credit/%252B8VMWj/YBC%252FNj3l/fetch/options

Is there a way where I can encode and escape the /?

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Monish Das
  • 383
  • 2
  • 12
  • 28
  • possible duplicate of [If I am encoding a URI which will be used as a query string parameter: encodeURI or encodeURIComponent](http://stackoverflow.com/questions/12555230/if-i-am-encoding-a-uri-which-will-be-used-as-a-query-string-parameter-encodeuri) – Reactgular Dec 29 '14 at 17:10
  • I am not using it as a query parameter.. i am using it as path parameter.. thats whats the issue..if i am using it as path param then it fails with 404 as its not able to escape / – Monish Das Dec 29 '14 at 18:24
  • Do you even know what makes a valid URL? http://www.w3.org/Addressing/URL/3_Requirements.html – Reactgular Dec 30 '14 at 00:02
  • Seriously, your question's example contains payment details for a transaction and you want to bypass URL specifications that say encoding a forward slash is a security risk. You don't work for Sony do you? – Reactgular Jan 05 '15 at 19:55
  • 1
    I have already raised this issue with my team and its upto them to decide. i have given them my solution and its their decision..i dont feel happy about using this but i have to – Monish Das Jan 05 '15 at 21:42

1 Answers1

0

Yes, its called encodeURIComponent

var a = 'http://monish.home.com/payment/9a2c1ae67d4ff85e561679fcff/credit/%252B8VMWj/YBC%252FNj3l/fetch/options'
encodeURIComponent(a); // result: "http%3A%2F%2Fmonish.home.com%2Fpayment%2F9a2c1ae67d4ff85e561679fcff%2Fcredit%2F%25252B8VMWj%2FYBC%25252FNj3l%2Ffetch%2Foptions"
SoluableNonagon
  • 11,541
  • 11
  • 53
  • 98
  • I dont think it works.. i tried it but doesnt work for forward slash / – Monish Das Dec 29 '14 at 18:24
  • what doesn't work? This encodes the / just as you asked – SoluableNonagon Dec 29 '14 at 18:38
  • try it in your console – SoluableNonagon Dec 29 '14 at 18:39
  • I have fixed this issue and there is a peculiar solution to this.. 1. You have encode the creditId encodeURIComponent() for eg encodeURIComponent(creditId) 2. Secondly you have to encode the whole url in service using encodeURI() for eg encodeURI('/payment/'+id+'/credit/'+creditId+'/fetch/options') ..remember here encodeURIComponent doesnt work hence use the first option of encodeURI().. Now this sanitizes the whole url and will work fine – Monish Das Dec 29 '14 at 20:57