38 lines
888 B
Bash
38 lines
888 B
Bash
#!/bin/bash
|
|
|
|
# API base URL
|
|
API_BASE_URL="http://localhost:8080/api/v1"
|
|
|
|
# Login credentials
|
|
USERNAME="boxuser"
|
|
PASSWORD="boxuser"
|
|
|
|
# Function to make an authenticated request
|
|
function authenticated_request() {
|
|
local method=$1
|
|
local endpoint=$2
|
|
local data=$3
|
|
|
|
# Get a new JWT token
|
|
TOKEN=$(curl -s -X POST -H "Content-Type: application/json" \
|
|
-d "{\"username\":\"$USERNAME\", \"password\":\"$PASSWORD\"}" \
|
|
"$API_BASE_URL/login" | jq -r '.token')
|
|
|
|
# Make the authenticated request
|
|
curl -s -X $method -H "Authorization: Bearer $TOKEN" \
|
|
-H "Content-Type: application/json" \
|
|
-d "$data" \
|
|
"$API_BASE_URL$endpoint"
|
|
}
|
|
|
|
# Get all boxes
|
|
response=$(authenticated_request "GET" "/boxes" "")
|
|
|
|
# Check if the request was successful
|
|
if [[ $? -eq 0 ]]; then
|
|
# Pretty print the boxes using jq
|
|
echo "$response" | jq .
|
|
else
|
|
echo "Error: Failed to get boxes."
|
|
fi
|