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=/;
You rock. Exactly what I was looking for. Thank you!
You’re welcome :)