encodeURIComponent in ruby

how to perform encodeURIComponent in ruby

February 21, 2018 - 1 minute read -
web note

Purpose

For my project jsdomrenderer deployed in AWS lambda, recently I met some bug caused by the ruby client. The url parameter passed in was wrong, and looks like being encoded twice. (ex: ‘%’ should be encoded as ‘%25’, but ruby’s URI::encode_www_form_component returns ‘%2525’. The javascript’s encodeURIComponent works fine in this case, that’s why I have this title)

Solution

Based on the stackoverflow discussion, it gives out

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

to replace encode_www_form_component. The regular expression gives out the following pattern for match: /\[^\-\_.!~\*'()a-zA-Z\d\]/. Where does this pattern comes from? Well, let’s look into encodeURIComponent’s implementation. It escapes everything except A-Z a-z 0-9 - _ . ! ~ * ' ( ). Also notice that other language’s default encode/decode uri component functions might have slight differences. Be sure to check some corner cases before your service goes to the production.

Related posts