ns_urlencode - Encode a string to its URL-encoded representation
This command encodes a string according to the rules for URL encoding defined in RFC #1738, Uniform Resource Locators. ns_urlencode codes essentially all non-alphanumeric characters in the specified components. The epcial octets are encoded by a "%" followed by the two-character hexa- decimal representation for the octet.
The option charset can be used to specify the character set of the encode operation. Option part is used to specify the query (default) or path encoding. In the query mode, the components will be concatenated via "&", in the path mode the components are concatenated with "/".
The following example encodes the Tcl dict as application/x-www-form-urlencoded POST data (https://www.w3.org/TR/html401/interact/forms.html#h-17.13.4)
set data { first_name John second_name Doe data "Hello World" } set post_data {} foreach {key value} $data { lappend post_data "[ns_urlencode -part query $key]=[ns_urlencode $value]" } set post_data [join $post_data &]
The second example shows encodings of path segments based on example 1 and 2 of https://www.w3.org/Addressing/URL/4_URI_Recommentations.html
% set e1 {albert bertram marie-claude} % ns_urlencode -part path {*}$e1 albert/bertram/marie-claude % set e2 {albert bertram/marie-claude} % ns_urlencode -part path {*}$e2 albert/bertram%2fmarie-claude