-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 /?

5
  • possible duplicate of If I am encoding a URI which will be used as a query string parameter: encodeURI or encodeURIComponent Commented Dec 29, 2014 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 / Commented Dec 29, 2014 at 18:24
  • Do you even know what makes a valid URL? w3.org/Addressing/URL/3_Requirements.html Commented Dec 30, 2014 at 0: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? Commented Jan 5, 2015 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 Commented Jan 5, 2015 at 21:42

1 Answer 1

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"
Sign up to request clarification or add additional context in comments.

4 Comments

I dont think it works.. i tried it but doesnt work for forward slash /
what doesn't work? This encodes the / just as you asked
try it in your console
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.