-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcurlj
executable file
·60 lines (51 loc) · 1.47 KB
/
curlj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#! /bin/sh
if [ "$1" = "--help" ] || [ "$1" = "-h" ] || [ "$1" = "help" ] || [ -z "$2" ]; then
cat <<EOF
$ curlj METHOD URL DATA
curlj[son]
Send a curl request with json data
Parameters:
\$1: http method, uppercase
\$2: url
[\$3]: data, valid json/javascript object
Flags:
-e: echo mode, print the resulting command
Examples:
$ curlj PUT "http://localhost:8092/key" '{"data":"ok"}'
$ curlj PUT "http://localhost:8092/key" "{data:'ok'}"
$ curlj -e PUT "http://localhost:8092/key" '{"data":"ok"}'
$ curlj GET "http://localhost:8092/key"
EOF
exit
fi
command -v curl >/dev/null || { echo "curl is not installed" 1>&2; exit 127; }
command -v jq >/dev/null || { echo "jq is not installed" 1>&2; exit 127; }
command -v node >/dev/null || { echo "node is not installed" 1>&2; exit 127; }
ECHO_MODE=0
if [ "$1" = "-e" ]; then
ECHO_MODE=1
shift
fi
METHOD=$1
URL="$2"
DATA=
if [ -n "$3" ]; then
DATA=`node -e 'console.log(JSON.stringify(eval("("+process.argv[1]+")")))' "$3" 2>/tmp/curlj_err`
if [ "$?" -gt 0 ]; then
echo "Error while parsing json:\n$(cat /tmp/curlj_err | head -n3 | tail -n2)"
exit 1
fi
fi
if [ "$METHOD" = "GET" ]; then
if [ "$ECHO_MODE" -eq 1 ]; then
echo "curl -Ss \"$URL\""
else
curl -Ss "$URL" | jq
fi
else
if [ "$ECHO_MODE" -eq 1 ]; then
echo "curl -Ss -X $METHOD \"$URL\" -d '$DATA' -H \"Content-Type: application/json\""
else
curl -Ss -X $METHOD "$URL" -d "$DATA" -H "Content-Type: application/json" | jq
fi
fi