-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.sh
More file actions
executable file
·55 lines (47 loc) · 954 Bytes
/
client.sh
File metadata and controls
executable file
·55 lines (47 loc) · 954 Bytes
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
#!/bin/sh
if [[ "$#" -eq 0 ]]
then
echo "Usage: client <endpoint> <method=GET>"
exit 1
fi
data=""
editinput(){
# tempf=$(mktemp /tmp/tmp.XXXXXXX.json)
tempf="input.tmp.json"
$EDITOR $tempf
data=$(cat $tempf)
# rm $tempf
}
conttype=""
body=""
response=""
methodparam=$(echo $2 | tr '[:lower:]' '[:upper:]')
case $methodparam in
GET|POST|PUT|DELETE)
method="-X$methodparam";;
*)
method="";;
esac
case $methodparam in
POST|PUT)
editinput
if [[ $data == "" ]]
then
echo "[ABORT] Request Cancelled"
exit 1
fi
response=$(curl -svL -H 'Content-Type: application/json' -d "$data" $method http://127.0.0.1:8000/$1);;
DELETE)
response=$(curl -svL $method http://127.0.0.1:8000/$1);;
*)
response=$(curl -svL http://127.0.0.1:8000/$1);;
esac
html=$(echo "$response" | w3m -dump -T text/html)
json=$(echo "$response" | jq 2> /dev/null)
exit_code="$?"
if [[ "$exit_code" -eq 0 ]]
then
echo "$json"
else
echo "$html"
fi