How to have url not encoded in rails - ruby-on-rails

I don't understand why all my special characters in my url are encoded for example :
new_subscription_url(:session_id => '{CHECKOUT_SESSION_ID}' )
Give me
http://localhost:3000/en/subscriptions/new?session_id=%7BCHECKOUT_SESSION_ID%7D
All special characters are encode. How could I have them not encoded ?

It is not encoded but rather escaped.
According to Internet standard (IETF section 2.4), URI is always in an "escaped" form.
On the side note, if you want to unescape it, you can use
CGI::unescape(new_subscription_url(session_id: '{CHECKOUT_SESSION_ID}' ))

Related

Passing "%20" into a string to be encoded as a URL without it converting to a space?

I have the following code to post data to a site: https://play.golang.org/p/e1g0Nd1kDh0
When I view the request in Fiddler, it shows as:
"jobTitle=Area Manager"
What I want it to do is send the string exactly as it is in the code (i.e. not encode the %20 to spaces), as it seems to be causing some confusion on the other side? An identical request made using a Python program works fine where the spaces are not added.
I've tried escaping it by doubling the % signs, but it doesn't seem to work. Any help would be great.
Thanks.
If you're trying to receive a literal %20 on the server side, then encode the % sign. It encodes to %25. So your postdata becomes:
data := "&jobTitle=Area%25%20Manager"
But if this is happening, there is probably a problem on the server side where the postdata is being decoded twice.
You can also pass the URL encode characters individually. In this case %25%32%30 = "%20"

Simple cipher to encode URL

I want to encode a url (http://www.someurl.com) so that I can add it as a parameter to a GET call:
domain.com/encoded_url
Once received on the other end, I need to be able to decode the url, however, security is not an issue. I rather stick to a simple cipher (must be able to handle special characters like ':' and '?') since the more advanced ciphers produce encoded strings that do not pass as valid URI strings and can't be passed as URL parameters.
What kind of cipher implemented in Ruby would work best for this purpose?
Use Base64.urlsafe_encode64
Base64 is a good idea to use, Ruby provides URL safe implementation of it, because the default will produce characters like + and =. urlsafe_encode64 will use '-' instead of '+' and '_' instead of '+' so they can be passed like URL parameters.
require "base64"
encoded_url = Base64.urlsafe_encode64('http://www.someurl.com', ) #=> "aHR0cDovL3d3dy5zb21ldXJsLmNvbQ=="
decoded_url = Base64.urlsafe_decode64(encoded_url) #=> "http://www.someurl.com"
https://apidock.com/ruby/Base64/urlsafe_encode64
If you really need to encode it and security is not an issue, just use Base64 and then URI escape it.
But there's no need to, just uri escape it.
str = Base64.encode64(url)
encoded_str = URI.encode(str)
btw, this is a semi-possible duplicate
Ruby url encoding string

how to pass URL with whitespace in Chrome and IE or include whitespace without encode?

I have a url to pass on my website that have whitespace. what thing i should done that chrome and IE never encode them. suppose
Mywebsite.com/search/ASP.NET MVC 2
IE and chrome fill whitespace with %20 how i can stop them to do this type of things.
You can replace the whitespace with "_" - this is a pretty normal case. But you will probably not be able to keep your spaces.
Whitespaces are not allowed in URLs. That is why they can automatically encoded by some browser if you call that page directly.
What's the problem with that encoding on the receiver side? Just decode the data or (if you are sure there were only whitespaces) just replace all %20 with a whitespace.

Can backslash be encoded correctly in URL with URL rewrite?

I am working on an ASP.NET MVC2 project. The problem is when a string which would be rewritten into URL contains a special character such as backslash or question mark, the URL will be wrong, even if I have encoded it before.
For example:
I have a product id "p001\2-2".
I encoded it into "p001%5C2-2"
The URL http://domain.com/Product/p001%5C2-2 responds HTTP Error 400 - Bad Request.
How can I get it correct?
Try to use Html.Encode to resolve your backslash.
If the backslash is the only 'special' character in your id, you could use Replace("%5C","\").
Have you checked your routingMap? there has to be a route like
Product/{prodictID}
I had a similar problem with %2F in my URLs. Try appending the nOrmalize flag to your rewriteRule.
Example with normalize flag "O" in bold:
RewriteRule ^(.*)index\.html?$ http://www.yoursite.com/$1 [R=301,L,**O**]

Can we use & in url?

Can we use "&" in a url ? or should "and" be used?
Yes, you can use it plain in your URL path like this:
http://example.com/Alice&Bob
Only if you want to use it in the query you need to encode it with %26:
http://example.com/?arg=Alice%26Bob
Otherwise it would be interpreted as argument separator when interpreted as application/x-www-form-urlencoded.
See RFC 3986 for more details.
An URL is generally in the form
scheme://host/some/path/to/file?query1=value&query2=value
So it is not advisable to use it in an URL unless you want to use it for parameters. Otherwise you should percent escape it using %26, e.g.
http://www.example.com/hello%26world
This results in the path being submitted as hello&world. There are other characters which must be escaped when used out of context in an URL. See here for a list.
Unless you're appending variables to the query string, encode it.
encode '&' with & (this answer is based on your use of tags)
If you are asking what to use "&" or "and" when registering the name of your URL, I would use "and".
EDIT: As mentioned in comments "& is an HTML character entity and not a URI character entity. By putting that into a URI you still have the ampersand character and additional extraneous characters." I started answering before fully understanding your question.

Resources