NaviServer Programmable Web Server

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

ns_percentencode(n) 5.1.0 naviserver "NaviServer Built-in Commands"

Name

ns_percentencode - Encoding and Decoding Percent-Encoded Strings

Table Of Contents

Synopsis

Description

This group of commands lets you encode and decode strings by escaping unsafe characters as percent-escape sequences ("%xx"). ns_percentencode and ns_percentdecode are the low-level percent-encoding primitives. They operate on a single string according to a selected encoding scheme. The supported schemes are query, path, cookie, and oauth1. ns_urlencode and ns_urldecode are URL-oriented convenience commands. They use URL parts rather than percent-encoding schemes. In addition to the historical query and path parts, ns_urlencode supports higher-level URL construction helpers such as fullpath and fragment. Use ns_percentencode when you need a primitive percent encoder for one string. Use ns_urlencode when constructing URL query strings, path components, full paths, or fragments from application data.

COMMANDS

ns_percentencode ?-charset value? ?-scheme query|path|cookie|oauth1? ?-uppercase? ?--? string

Percent‑encodes a single string. All characters not in the selected scheme’s unreserved set are replaced by "%" and two hex digits.

ns_percentdecode ?-charset value? ?-fallbackcharset value? ?-scheme query|path|cookie|oauth1? ?--? string

Reverses the percent‑encoding in a string. Under query scheme only, "+" is converted back to space. If decoding to UTF‑8 fails and -fallbackcharset is given (and -charset is not), a second decode attempt uses the fallback charset instead of raising an exception.

ns_urlencode ?-charset value? ?-part query|path|fullpath|fragment|cookie|oauth1? ?-uppercase? ?--? component ...

Encodes URL data according to the selected URL -part. With -part query, a single argument is encoded as one query value, preserving the historical behavior. With multiple arguments, the arguments are interpreted as alternating name/value pairs. Names and values are encoded and joined as a query string using "=" and "&". Repeated names are preserved when the argument list contains repeated names, for example when using ns_set data via ns_set array. With -part path, each argument is treated as one URL path component and multiple arguments are joined with "/". This is the historical behavior of ns_urlencode for path data. With -part fullpath, the single argument is treated as a full URL path. Slash characters in the argument are preserved as path separators, and each path segment is encoded individually. With -part fragment, the single argument is encoded as a URL fragment component. The cookie and oauth1 parts are accepted for backward compatibility, but are deprecated for ns_urlencode. Use ns_percentencode with -scheme cookie or -scheme oauth1 instead.

ns_urldecode ?-charset value? ?-fallbackcharset value? ?-part query|path|fullpath|fragment|cookie|oauth1? ?--? string

Decodes percent-escape sequences in string according to the selected URL -part. The supported part names match ns_urlencode. For decoding, the selected part primarily controls the treatment of plus signs. With -part query, plus signs are decoded as spaces. With path, fullpath, fragment, cookie, and oauth1, plus signs remain literal plus signs. Percent escape sequences are decoded in all parts. The cookie and oauth1 parts are accepted for backward compatibility, but are deprecated for ns_urldecode. Use ns_percentdecode with -scheme cookie or -scheme oauth1 instead.

PERCENT-ENCODING SCHEMES AND URL PARTS

The URL parts accepted by ns_urlencode and ns_urldecode are query, path, fullpath, fragment, cookie, and oauth1. The cookie and oauth1 parts are retained for compatibility only and are deprecated in favor of the corresponding ns_percentencode and ns_percentdecode schemes.

OPTIONS

-scheme query|path|cookie|oauth1

Select the percent-encoding scheme for ns_percentencode or ns_percentdecode.

-part query|path|fullpath|fragment|cookie|oauth1

Select the URL part for ns_urlencode or ns_urldecode. For ns_urlencode, query, path, fullpath, and fragment provide URL-oriented encoding behavior. For ns_urldecode, the same part names are accepted; query decoding maps "+" to space, while the other parts preserve literal plus characters. The cookie and oauth1 parts are deprecated compatibility aliases. Use ns_percentencode or ns_percentdecode with -scheme instead.

-fallbackcharset value

On decode, if the result is invalid UTF‑8 and -charset is not given, retry using this charset instead of raising an exception.

-uppercase

Output hex digits A–F in uppercase ("%2F" instead of "%2f").

--

Marks the end of options; any following arguments (even starting with "-") are treated as data.

ns_urlencode encodes unencoded application data into URL parts. It does not preserve existing percent escapes as escapes; a literal "%" character is encoded when it is unsafe for the selected part. Do not pass already percent-encoded URL parts to ns_urlencode unless double encoding is intended.

EXAMPLES

Encode form data as an application/x-www-form-urlencoded query string or POST body: (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 [ns_urlencode -part query {*}$data]
 ## output: first_name=John&second_name=Doe&data=Hello+World

Encode path segments per W3C URI recommendations (examples 1 and 2 of URI Recommendation):

 set e1 {albert bertram marie-claude}
 ns_urlencode -part path {*}$e1
 ## output: albert/bertram/marie-claude
 
 set e2 {albert bertram/marie-claude}
 ns_urlencode -part path {*}$e2
 ## output: albert/bertram%2fmarie-claude
 ns_urlencode -part fullpath "/albert/bertram marie-claude"
 ## output: /albert/bertram%20marie-claude

Encode a URL fragment:

 ns_urlencode -part fragment "section 1#details"
 ## output: section%201%23details

By default escapes use lowercase, but you can request uppercase:

 % ns_percentencode -scheme path "a/b c"
 ## output: a%2fb%20c
 % ns_percentencode -scheme path -uppercase "a/b c"
 ## output: a%2FB%20c

Use the cookie scheme to percent‑encode only those bytes unsafe in cookie segments:

 ns_percentencode -scheme cookie "key=value; path=/"
 ## output:  key%3dvalue%3b%20path%3d%2f

Construct an OAuth 1.0 signature base string

 ns_percentencode -scheme oauth1 -uppercase "Ladies + Gentlemen"
 ## output: Ladies%20%2B%20Gentlemen

If your percent‐encoded input isn’t valid UTF‑8, you can retry with a different charset:

 ns_percentdecode "%C3%A9t%C3%A9"
 ## output: été
 
 ns_percentdecode "%E9"
 ## output: input string '%E9' cannot be converted to UTF-8
 
 ns_percentdecode -charset iso8859-1 "%E9"
 ## output: é

With the fallback charset, a valid UTF-8 string and a latin-1 string can be decoded.

 ns_percentdecode -fallbackcharset iso8859-1 "%C3%A9t%C3%A9"
 ## output: été
 
 ns_percentdecode -fallbackcharset iso8859-1 "%E9"
 ## output: é

See Also

ns_charsets, ns_http, ns_urlcharset, nsd

Keywords

URL, charset, encoding, global built-in, percent-encoding