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 offold
to theread
command - The
while
command accepts one line at a time (which is only 1 character wide) from theread
command, which gets its input fromfold
, and assigns it to variablec
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
Why Do I Get "Suspended (Tty Output)" in One Terminal But Not in Others
Create a Virtual Floppy Image Without Mount
Microsecond Accurate (Or Better) Process Timing in Linux
How to Install R 3.1.2 on Linux Mint 17.1
Is There Any Shortcut to Reference the Path of the First Argument in a Mv Command
How to Set Cronjob with Non-Root User
Nasm - Symbol 'Printf' Causes Overflow in R_X86_64_Pc32 Relocation
How to Search an Image for Subimages Using Linux Console
Syntax With Pound and Percent Sign After Shell Parameter Name
Could Not Load Shared Library Symbols for Linux-Vdso.So.1. While Debugging
Use a C Library in Swift on Linux
What Is the Purpose of Features.H Header
How to Setup a Periodic Timer Callback in a Linux Kernel Module
Multiple Option Arguments Using Getopts (Bash)
X86 Assembly: Before Making a System Call on Linux Should You Save All Registers