Cli tool to encode stdin to URL.
This tool is a thin wrapper around the standard go escape functions PathEscape
and QueryEscape
from net/url
.
$ ./urlencode.exe -h
urlencode (version: v0.7.0)
This program is a thin wrapper around the standard go url escape functions.
Available flags:
-input string
string to escape, if empty (default) read from stdin
-keep-spaces
keep spaces as they are
-path-escape
use PathEscape instead of QueryEscape
-trim
trim (from both sides) whitespaces and newlines
$ urlencode -input " Ceci est un message étrange "
+Ceci+est+un+message+%C3%A9trange+
$ echo " Ceci est un message étrange " | urlencode
+Ceci+est+un+message+%C3%A9trange+%0A
$ echo " Ceci est un message étrange " | urlencode -keep-spaces
Ceci est un message %C3%A9trange %0A
$ echo " Ceci est un message étrange " | urlencode -path-escape
%20Ceci%20est%20un%20message%20%C3%A9trange%20%0A
$ echo " Ceci est un message étrange " | urlencode -trim
Ceci+est+un+message+%C3%A9trange
Note : echo
adds a new line at the end that is visible as %0A
if no trim option is used.
The url encoding is not the same before the ?
and after it.
- Before the
?
the spaces should be encodes as%20
(and not+
). This part is encoded withPathEscape
go function. - After the
?
the spaces could be encodes as+
(%20
is also possible but is longer ). This part is encoded withQueryEscape
go function.
For mode info you can check this answer at SX.
If you want to encode some text in data:
uri, for example in the case of data:image/xml+svg, ...
you can use urlencode -path-escape -keep-spaces -trim
.