Urlencode and urldecode from a command line

by Ruslan Spivak on June 2, 2010

Recently I have worked with a REST Web service and had to URL encode a lot of values from the command line.

If all you need is to URL encode data for a POST request you can use curl with –data-urlencode parameter, but I needed to encode values that went into a URL query string.
So for the task I wrote a short code in Python to do just that (there are examples available in BASH or Perl, but this is a pure Python using only standard library):

python -c "import sys, urllib as ul; print ul.quote_plus(sys.argv[1])"

Assign it to an alias by putting single quotes around the expression and you’re good to go:

$ alias urlencode='python -c "import sys, urllib as ul; print ul.quote_plus(sys.argv[1])"'

Example:

$ urlencode 'q werty=/;'
q+werty%3D%2F%3B

And the decode part:

python -c "import sys, urllib as ul; print ul.unquote_plus(sys.argv[1])"
$ alias urldecode='python -c "import sys, urllib as ul; print ul.unquote_plus(sys.argv[1])"'
$ urldecode 'q+werty%3D%2F%3B'
q werty=/;
If you enjoyed this post why not subscribe via email or my RSS feed and get the latest updates immediately. You can also follow me on GitHub or Twitter.

{ 13 comments… read them below or add one }

Gabriel October 12, 2011 at 12:43 PM

You rock. Exactly what I was looking for. Thank you!

Reply

Ruslan Spivak October 12, 2011 at 8:27 PM

You’re welcome :)

Reply

Gregory Rasdi January 20, 2012 at 7:45 AM

Hi Ruslan, lovely work I love this website great stuff!

Reply

Peter Jenkins February 21, 2012 at 2:44 PM

Handy. Replace “sys.argv[1]” with “sys.stdin.read()” to use in a pipeline.

Reply

Erik Lundmark November 14, 2012 at 4:18 PM

If you want the node.js version of this:
alias urlencode_js='node -e "console.log(encodeURIComponent(process.argv[1]))"'
alias urldecode_js='node -e "console.log(decodeURIComponent(process.argv[1]))"'

Reply

url encode June 22, 2013 at 11:38 AM

great tutorial. thanks

Reply

Keith Kim June 26, 2013 at 3:15 PM

Hey Ruslan, what a coincidence my finding your blog!

Nice one. I needed to do just this, but with binary data and found I could do this to omit final newline:

python -c “from __future__ import print_function; import sys, urllib as ul; print(ul.unquote_plus(sys.argv[1]), end=”)” ‘%0a%02%08%01%18%01%22%04%0a%02%08%02′ >data.bin

Reply

Ruslan Spivak June 28, 2013 at 6:43 PM

Cool :)

Reply

Aleksei November 26, 2013 at 10:01 AM

Sometimes one needs to make ALL url-encoded filenames as decoded – below – my solution:

first file – urldecode.sh (make sure it is executable – chmod +x urldecode.sh)
##start content
#!/bin/bash
python -c “import sys, urllib as ul; print ul.unquote_plus(sys.stdin.read())”
##end content

second file – urldecoder.sh(make sure it is executable)
##start content
#!/bin/bash

function readfile()
{
a=0
arr=()
while read line
do
a=$(($a+1));
arr[$a]=$line;
done > encode.txt
find $1 -name “*.*” | xargs ls | “$urldecode” >> norm.txt

readfile “encode.txt”
encodeArr=(“${outArr[@]}”)
readfile “norm.txt”
normArr=(“${outArr[@]}”)

k=0

for i in “${encodeArr[@]}”
do
mv -n “$i” “${normArr[k]}”
k=$(($k+1));
done
##end content

how to use:
./urldecoder.sh path-to-folder-containing-files-with-encoded-filenames

it may produce messages like:
mv: `../Desktop/ticket/993/screen.png’ and `../Desktop/ticket/993/screen.png’ are the same file
but it is no matter – cosider it as a warn

Thanks

Reply

Nikhil June 9, 2014 at 7:09 PM

Thank you!

Reply

vasya July 27, 2014 at 3:05 AM

/Users/jt3k/canonical/u1-files/Ubuntu One/Recovered > ./urldecoder.sh “/Users/jt3k/canonical/u1-files/Ubuntu One/Recovered”
./urldecoder.sh: line 26: syntax error: unexpected end of file

any ideas?

Reply

Aleksei August 5, 2014 at 11:14 AM

sorry, there is was an error in my original post – below – updated correct content

===urldecoder.sh

#!/bin/bash

function readfile()
{
a=0
arr=()
while read line
do
a=$(($a+1));
arr[$a]=$line;
done > encode.txt
find $1 -name “*.*” | xargs ls | “$urldecode” >> norm.txt

readfile “encode.txt”
encodeArr=(“${outArr[@]}”)
readfile “norm.txt”
normArr=(“${outArr[@]}”)

k=0

for i in “${encodeArr[@]}”
do
mv -n “$i” “${normArr[k]}”
k=$(($k+1));
done

Reply

Vladimir December 15, 2014 at 9:39 PM

How to pipe output from another command to this alias?

Reply

Speak your mind

Previous post:

Next post: