NaviServer - programmable web server
4.99  5.0

[ Main Table Of Contents | Table Of Contents | Keyword Index ]

ns_urlencode(n) 4.99.30 naviserver "NaviServer Built-in Commands"

Name

ns_urlencode - Encode a string to its URL-encoded representation

Table Of Contents

Synopsis

Description

This command encodes a string according to the rules for URL encoding defined in RFC 3986 Uniform Resource Identifier (URI), (or RFC 1738, when NaviServer was configured to use this older standard). 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.

COMMANDS

ns_urlencode ?-charset charset? ?-part part? ?--? components

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 part argument can be as well used for more percent encodings such as the cookie encoding (value "cookie", RFC 6265) or the oauth1 encoding (value "oauth1") as defined in RFC 5849 Section 3.6 for the construction of the * signature base string and the "Authorization" header field.

EXAMPLES

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

See Also

ns_charsets, ns_http, ns_urlcharset, ns_urldecode, nsd

Keywords

charset, encoding, global built-in, url