How to Raw Url Encode/Decode in JavaScript and Ruby to Get the Same Values in Both

How do I raw URL encode/decode in JavaScript and Ruby to get the same values in both?

Use

URI.escape(foo, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))

in ruby, and

encodeURIComponent(foo); 

in javascript

Both these will behave equally and encode space as %20.

Ruby equivalent to JavaScript’s encodeURIComponent that produces identical output?

URI.escape(foo, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))

found here: How do I raw URL encode/decode in JavaScript and Ruby to get the same values in both?

URI.escape is obslete since last I posted. As per suggested comment, now use: ERB::Util.url_encode or CGI.escape

How do I encode/decode HTML entities in Ruby?

HTMLEntities can do it:

: jmglov@laurana; sudo gem install htmlentities
Successfully installed htmlentities-4.2.4
: jmglov@laurana; irb
irb(main):001:0> require 'htmlentities'
=> []
irb(main):002:0> HTMLEntities.new.decode "¡I'm highly annoyed with character references!"
=> "¡I'm highly annoyed with character references!"

How to decode HTML entities in JavaScript using in a Ruby on Rails project

Ok, the answer to MY question is:

var curData = <%=raw BuildCompact.getData("ruby", "rubinius", 500).to_json %>;

When should space be encoded to plus (+) or %20?

+ means a space only in application/x-www-form-urlencoded content, such as the query part of a URL:

http://www.example.com/path/foo+bar/path?query+name=query+value

In this URL, the parameter name is query name with a space and the value is query value with a space, but the folder name in the path is literally foo+bar, not foo bar.

%20 is a valid way to encode a space in either of these contexts. So if you need to URL-encode a string for inclusion in part of a URL, it is always safe to replace spaces with %20 and pluses with %2B. This is what, e.g., encodeURIComponent() does in JavaScript. Unfortunately it's not what urlencode does in PHP (rawurlencode is safer).

See Also

HTML 4.01 Specification application/x-www-form-urlencoded

How to urlencode data for curl command?

Use curl --data-urlencode; from man curl:

This posts data, similar to the other --data options with the exception that this performs URL-encoding. To be CGI-compliant, the <data> part should begin with a name followed by a separator and a content specification.

Example usage:

curl \
--data-urlencode "paramName=value" \
--data-urlencode "secondParam=value" \
http://example.com

See the man page for more info.

This requires curl 7.18.0 or newer (released January 2008). Use curl -V to check which version you have.

You can as well encode the query string:

curl --get \
--data-urlencode "p1=value 1" \
--data-urlencode "p2=value 2" \
http://example.com
# http://example.com?p1=value%201&p2=value%202

How to find out if string has already been URL encoded?

Decode, compare to original. If it does differ, original is encoded. If it doesn't differ, original isn't encoded. But still it says nothing about whether the newly decoded version isn't still encoded. A good task for recursion.

I hope one can't write a quine in urlencode, or this algorithm would get stuck.

Exception: When a string contains "+" character url decoder replaces it with a space even though the string is not url encoded

Pass a percent (%) sign in a url and get exact value of it using php

Answer:

To send a % sign in a url, instead send %25.

In your case, in order for php to see a percent sign, you must pass the character string %25B6011000995504101^SB to the server.

Why:

In URLs, the percent sign has special meaning. Is used to encode special characters. For example, & is the separator between parameters, so if you want your parameter to actually contain an &, you instead write %26. Because the percent sign is used to encode special characters, it is also a special character, and so if you want to actually send a percent sign, it must also be encoded. The encoding for a percent sign is %25.

Encrypting data with ruby decrypting with node

OK. I want to thank everyone for helping me out. Basically this thread here answers my question: https://github.com/joyent/node/issues/1395. I am going to go ahead and post the two programs in case anyone else has to go through this rigamarole. Keep in mind this isn't mean to be hardcore secure, this is a stepping stone for ruby encrypting data and node decrypting it. You will have to take more steps to make sure higher security measures are taken.

The code is located at this gist: https://gist.github.com/799d6021890f34734470

These were run on ruby 1.9.2p290 and node 0.4.10



Related Topics



Leave a reply



Submit