Skip to content

Instantly share code, notes, and snippets.

@cyb3rd4d
Last active August 29, 2015 14:22
Show Gist options
  • Save cyb3rd4d/c93d24d5379c9b8009dd to your computer and use it in GitHub Desktop.
Save cyb3rd4d/c93d24d5379c9b8009dd to your computer and use it in GitHub Desktop.
Fetch data from MySQL and send a cURL request in Bash.
#!/bin/bash
urlencode() {
# urlencode <string>
# from https://gist.github.com/cdown/1163649
local length="${#1}"
for (( i = 0; i < length; i++ )); do
local c="${1:i:1}"
case $c in
[a-zA-Z0-9.~_-]) printf "$c" ;;
*) printf '%%%02X' "'$c"
esac
done
}
MYSQL_HOST=''
MYSQL_USER=''
MYSQL_PASSWORD=''
MYSQL_DB=''
data=$(mysql -B -N -h ${MYSQL_HOST} -u ${MYSQL_USER} -p${MYSQL_PASSWORD} -D ${MYSQL_DB} -e "SELECT
ta.some_column as column1,
ta.another_column as column2
FROM
some_table ta
WHERE
ta.id = 6;" | sed "s/$(printf '\t')/,/g")
IFS="," read -a result <<< "$data"
property1=$(urlencode "${result[0]}")
property2=$(urlencode "${result[1]}")
post_data="field1=${property1}&field2=${property2}"
curl -XPOST http://api.service.net/get/resource --data "${post_data}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment