Skip to content

Instantly share code, notes, and snippets.

@mattkenefick
Last active March 13, 2022 10:39
Show Gist options
  • Save mattkenefick/7e565607c9656ad81d4d to your computer and use it in GitHub Desktop.
Save mattkenefick/7e565607c9656ad81d4d to your computer and use it in GitHub Desktop.
Bash CRUD (Get,Post,Put,Delete) Commands
# Polymer Mallard - Bash CRUD
#
# Put the following into your ~/.bash_rc or ~/.bash_profile
# Requires Python, cURL
function pmCrudRequest {
printf " \n \e[1;33m$method\e[0m from \e[1;33m$url\e[0m \n \n"
printf " \n \e[0;32m"
echo "$response" | python -m json.tool
printf "\e[0m"
}
# $ GET "http://www.example.com/api/endpoint"
# --
GET() {
local method="GET"
local url=$1
local response=$(curl -# -X GET "$1")
pmCrudRequest;
}
# $ POST "http://www.example.com/api/endpoint foo=bar key=value option=answer
# --
POST() {
local method="POST"
local url=$1; shift;
local curlparams="curl -# -X POST \"$url\""
while (( "$#" )); do
curlparams+=" -d $1"; shift;
done
local response=$(eval $curlparams)
pmCrudRequest;
}
# $ PUT "http://www.example.com/api/endpoint/2" foo=bar key=value option=answer
# --
PUT() {
local method="PUT"
local url=$1; shift;
local curlparams="curl -# -X PUT \"$url\""
while (( "$#" )); do
curlparams+=" -d $1"; shift;
done
local response=$(eval $curlparams)
pmCrudRequest;
}
# $ DELETE "http://www.example.com/api/endpoint/2"
# --
DELETE() {
local method="DELETE"
local url=$1
local response=$(curl -# -X DELETE "$1")
pmCrudRequest;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment