asUrlDecoded
Module Export
text |> textDecodes a percent-encoded URL query parameter value into text (RFC 3986).
asUrlDecoded turns percent-encoded text back into its original form, resolving each %XX escape and interpreting the resulting bytes as UTF-8. It is the inverse of asUrlEncoded.
Decoding follows RFC 3986, which means %20 becomes a space while a + is kept as a literal plus sign.
Examples
Decode a query parameter value
import { asUrlDecoded } from 'text'
from 'blue%20%26%20green%20shoes' asUrlDecoded
// Returns: 'blue & green shoes'Reserved characters are restored
import { asUrlDecoded } from 'text'
from 'a%20b%26c%3Dd%3Fe%2Ff%23g' asUrlDecoded
// Returns: 'a b&c=d?e/f#g'UTF-8 escapes become non-ASCII characters
import { asUrlDecoded } from 'text'
from '%C3%85ke%20p%C3%A5%20caf%C3%A9' asUrlDecoded
// Returns: 'Åke på café'Round-tripping
import { asUrlEncoded, asUrlDecoded } from 'text'
from 'blue & green? 100% på café +1!' asUrlEncoded asUrlDecoded
// Returns: 'blue & green? 100% på café +1!'Notes
- A
+is kept as a literal plus sign. If you are decoding anapplication/x-www-form-urlencodedpayload (an HTML form body), where+means space, replace it before decoding — see below - Sequences that aren’t valid escapes, such as
%zzor a trailing%, are left untouched rather than causing a runtime error - Decoding text that was never encoded is safe: text without
%sequences is returned unchanged
Decoding form-urlencoded values
For form-encoded input, turn + into a space first using replace:
import { replace, asUrlDecoded } from 'text'
from 'blue+%26+green' replace(/\+/, ' ') asUrlDecoded
// Returns: 'blue & green'See Also
- asUrlEncoded to percent-encode a value
- encode and decode for converting between text and bytes (Base64, hex, UTF-8). Those produce or consume
[byte], whereasasUrlDecodedistext |> text