#!/bin/bash # 定義變數以便於管理 API_URL="http://localhost:8080/api/v1/user" AUTH_TOKEN="Bearer -YSEvxm.BAy6LySE1-WXm4MbsYoBxlI-5NpCPp70" # 使用 curl 發送 HTTP 請求,帶有授權標頭 # -s: 靜默模式,不顯示進度條 # -f: 如果伺服器返回錯誤狀態碼,則失敗並返回非零狀態 # -o /dev/null: 將輸出丟棄(如果不需要顯示響應內容) # -w: 輸出 HTTP 狀態碼以便檢查 response_status=$(curl -s -f -X GET \ -H "Authorization: $AUTH_TOKEN" \ "$API_URL" \ -w "%{http_code}" \ -o /dev/null) # 檢查請求是否成功 if [ "$response_status" -eq 200 ]; then echo "Request successful (Status: $response_status)" # 如果需要顯示響應內容,可以移除 -o /dev/null 並直接輸出 # curl -s -X GET -H "Authorization: $AUTH_TOKEN" "$API_URL" else echo "Request failed (Status: $response_status)" exit 1 fi