Url Encoding a String in Bash Script

How to urlencode data for curl command?

Use curl --data-urlencode; from man curl:

This posts data, similar to the other --data options with the exception that this performs URL-encoding. To be CGI-compliant, the <data> part should begin with a name followed by a separator and a content specification.

Example usage:

curl \
--data-urlencode "paramName=value" \
--data-urlencode "secondParam=value" \
http://example.com

See the man page for more info.

This requires curl 7.18.0 or newer (released January 2008). Use curl -V to check which version you have.

You can as well encode the query string:

curl --get \
--data-urlencode "p1=value 1" \
--data-urlencode "p2=value 2" \
http://example.com
# http://example.com?p1=value%201&p2=value%202

URL encoding a string in bash script

You want $MESSAGE to be in double-quotes, so the shell won't split it into separate words, then pass it to PHP as an argument:

ENCODEDMESSAGE="$(php -r 'echo rawurlencode($argv[1]);' -- "$MESSAGE")"

URL encoding a string in shell script in a portable way

The functions below have been tested in BusyBox Ash, Dash, Bash and ZSH.

They only use shell builtins or core commands and should work in other shells as well.

urlencodepipe() {
local LANG=C; local c; while IFS= read -r c; do
case $c in [a-zA-Z0-9.~_-]) printf "$c"; continue ;; esac
printf "$c" | od -An -tx1 | tr ' ' % | tr -d '\n'
done <<EOF
$(fold -w1)
EOF
echo
}

urlencode() { printf "$*" | urlencodepipe ;}

How it works:

  • Standard input is processed by fold -w1, which re-formats its input so that it is 1 column wide (in other words, it adds a \n after each input character so that each character will be on its own line)
  • The here-document <<EOF feeds the output of fold to the read command
  • The while command accepts one line at a time (which is only 1 character wide) from the read command, which gets its input from fold, and assigns it to variable c
  • case tests if that character needs to be url encoded. If it doesn't, then it's printed and the while loop continues
  • od converts each input character to hex
  • tr converts spaces to % and joins multiple lines

How to url-encode all characters (special and non-special) in bash

I don't believe this is possible in pure Bash, but other shell utilities can be strung together to achieve this result, for example

urlencode() {
printf %s "$1" | od -An -tx1 -v -w${#1} | tr ' ' %
}
urlencode abcd0123 # => %61%62%63%64%30%31%32%33

Possible to urlencode a variable in a shell script?

Here is one method for URL encoding the shell string DATA:

DATA=$(python -c "import urllib, sys; print urllib.quote(sys.argv[1])"  "$DATA")

Here is another:

DATA=$(perl -MURI::Escape -e 'print uri_escape($ARGV[0]);' "$DATA")

How to urlencode data for curl command?

Use curl --data-urlencode; from man curl:

This posts data, similar to the other --data options with the exception that this performs URL-encoding. To be CGI-compliant, the <data> part should begin with a name followed by a separator and a content specification.

Example usage:

curl \
--data-urlencode "paramName=value" \
--data-urlencode "secondParam=value" \
http://example.com

See the man page for more info.

This requires curl 7.18.0 or newer (released January 2008). Use curl -V to check which version you have.

You can as well encode the query string:

curl --get \
--data-urlencode "p1=value 1" \
--data-urlencode "p2=value 2" \
http://example.com
# http://example.com?p1=value%201&p2=value%202


Related Topics



Leave a reply



Submit