-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi-test
More file actions
executable file
·31 lines (28 loc) · 1.12 KB
/
Copy pathapi-test
File metadata and controls
executable file
·31 lines (28 loc) · 1.12 KB
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
#!/bin/bash
if [ -z "$1" ]; then
echo "api-test v1.0.0 - Simple API Testing Tool"
echo ""
echo "Usage: api-test <url> [method] [headers] [body]"
echo ""
echo "Examples:"
echo " api-test https://api.example.com/users GET"
echo " api-test https://api.example.com/users POST '{\"name\":\"test\"}'"
echo " api-test https://api.example.com/users/1 DELETE"
exit 0
fi
URL="$1"
METHOD="${2:-GET}"
BODY="$3"
if [ "$METHOD" = "POST" ] || [ "$METHOD" = "PUT" ] || [ "$METHOD" = "PATCH" ]; then
if [ -n "$BODY" ]; then
curl -s -X "$METHOD" "$URL" \
-H "Content-Type: application/json" \
-d "$BODY" | python3 -m json.tool 2>/dev/null || curl -s -X "$METHOD" "$URL" -H "Content-Type: application/json" -d "$BODY"
else
curl -s -X "$METHOD" "$URL" -H "Content-Type: application/json" | python3 -m json.tool 2>/dev/null || curl -s -X "$METHOD" "$URL" -H "Content-Type: application/json"
fi
elif [ "$METHOD" = "DELETE" ]; then
curl -s -X DELETE "$URL" -w "\nHTTP Code: %{http_code}\n"
else
curl -s "$URL" | python3 -m json.tool 2>/dev/null || curl -s "$URL"
fi