Url

class Url extends Object

Percent-encoding, as URLs and form bodies use it. Both methods are called on the class itself; a Url object is never constructed.

The class is exported by the KS module:

#Import "Ks" { Url }

Table of Contents

Encode

Percent-encodes text.

Text := Url.Encode(Text , Encoding := "UTF-8")

Parameters

Text

Type: String

The text to encode.

Encoding

Type: String

The encoding the text's bytes are taken in, named as for FileEncoding. It defaults to UTF-8. A name which cannot be resolved throws a ValueError.

Return Value

Type: String

Returns the text with every byte outside the RFC 3986 unreserved set — A-Z a-z 0-9 - . _ ~ — percent-encoded. For example, Url.Encode("ä") returns %C3%A4, and Url.Encode("ab", "UTF-16") returns a%00b%00. A space becomes %20.

Decode

Resolves percent-escapes.

Text := Url.Decode(Text , Encoding := "UTF-8")

Parameters

Text

Type: String

The text to decode.

Encoding

Type: String

The encoding the escaped bytes are read in, named as for FileEncoding. Defaults to UTF-8.

Return Value

Type: String

Returns the decoded text.

A % which is not followed by two hexadecimal digits, and a +, are left unchanged.

Examples

#Import "Ks" { Url }

Url.Encode("a b&c=d")  ; "a%20b%26c%3Dd"
Url.Decode("a%20b%26c") ; "a b&c"
Url.Decode("100%")      ; "100%" — a lone % stands for itself

Builds a request URL with an encoded query value:

#Import "Ks" { Http, Url }

Res := Http.Get("https://example.com/search?q=" Url.Encode(Query))