cURL 與 HTTPie 實戰:命令列請求技巧
命令列 HTTP 工具是開發者必備技能。本篇將介紹 cURL 和 HTTPie 的實戰技巧。
一、 cURL 基礎
1.1 基本語法
bash
curl [options] URL1.2 常用選項
bash
# GET 請求
curl https://api.example.com/users
# 顯示回應標頭
curl -i https://api.example.com/users
# 只顯示標頭
curl -I https://api.example.com/users
# 詳細輸出(除錯)
curl -v https://api.example.com/users
# 靜默模式
curl -s https://api.example.com/users二、 cURL 進階
2.1 HTTP 方法
bash
# POST
curl -X POST https://api.example.com/users
# PUT
curl -X PUT https://api.example.com/users/1
# DELETE
curl -X DELETE https://api.example.com/users/1
# PATCH
curl -X PATCH https://api.example.com/users/12.2 請求標頭
bash
# 單一標頭
curl -H "Authorization: Bearer token" https://api.example.com/users
# 多個標頭
curl \
-H "Authorization: Bearer token" \
-H "Content-Type: application/json" \
-H "Accept: application/json" \
https://api.example.com/users2.3 請求 Body
bash
# JSON 資料
curl -X POST \
-H "Content-Type: application/json" \
-d '{"name": "John", "email": "john@example.com"}' \
https://api.example.com/users
# 從檔案讀取
curl -X POST \
-H "Content-Type: application/json" \
-d @data.json \
https://api.example.com/users
# Form 資料
curl -X POST \
-d "name=John&email=john@example.com" \
https://api.example.com/users
# Multipart Form
curl -X POST \
-F "file=@photo.jpg" \
-F "description=My photo" \
https://api.example.com/upload2.4 認證
bash
# Basic Auth
curl -u username:password https://api.example.com/users
# Bearer Token
curl -H "Authorization: Bearer token" https://api.example.com/users
# API Key
curl -H "X-API-Key: your-key" https://api.example.com/users2.5 Cookie
bash
# 發送 Cookie
curl -b "session=abc123" https://example.com
# 儲存 Cookie
curl -c cookies.txt https://example.com
# 載入 Cookie
curl -b cookies.txt https://example.com三、 cURL 實用技巧
3.1 格式化 JSON
bash
# 使用 jq
curl -s https://api.example.com/users | jq
# 篩選欄位
curl -s https://api.example.com/users | jq '.data[0].name'
# 格式化輸出
curl -s https://api.example.com/users | jq '.'3.2 下載檔案
bash
# 保留遠端檔名
curl -O https://example.com/file.zip
# 指定檔名
curl -o myfile.zip https://example.com/file.zip
# 續傳下載
curl -C - -O https://example.com/large-file.zip3.3 重導向處理
bash
# 跟隨重導向
curl -L https://short.url/abc
# 限制重導向次數
curl -L --max-redirs 5 https://short.url/abc3.4 超時設定
bash
# 連線超時
curl --connect-timeout 10 https://api.example.com
# 總超時
curl --max-time 30 https://api.example.com3.5 HTTPS 相關
bash
# 忽略 SSL 錯誤(不推薦)
curl -k https://self-signed.example.com
# 指定憑證
curl --cacert ca.crt https://api.example.com
# 使用客戶端憑證
curl --cert client.crt --key client.key https://api.example.com四、 HTTPie
HTTPie 是更人性化的命令列 HTTP 工具。
4.1 安裝
bash
# macOS
brew install httpie
# Ubuntu/Debian
apt install httpie
# pip
pip install httpie4.2 基本語法
bash
http [METHOD] URL [HEADERS] [BODY]4.3 基本使用
bash
# GET
http https://api.example.com/users
# POST JSON(預設)
http POST https://api.example.com/users name=John email=john@example.com
# 顯示請求和回應
http -v https://api.example.com/users4.4 資料類型語法
bash
# 字串(=)
http POST :3000/users name=John
# JSON 類型(:=)
http POST :3000/users age:=30 active:=true
# 陣列
http POST :3000/users tags:='["a", "b"]'
# 巢狀物件
http POST :3000/users address[city]=Taipei
# 從檔案讀取
http POST :3000/users @data.json4.5 標頭語法
bash
# 使用冒號
http :3000/users Authorization:'Bearer token' Accept:application/json
# 認證
http -a username:password :3000/users
# Bearer Token
http -A bearer -a token :3000/users4.6 表單上傳
bash
# Form 資料
http -f POST :3000/users name=John email=john@example.com
# 檔案上傳
http -f POST :3000/upload file@photo.jpg description=Photo五、 對比
5.1 相同操作對比
bash
# GET 請求
curl https://api.example.com/users
http https://api.example.com/users
# POST JSON
curl -X POST -H "Content-Type: application/json" \
-d '{"name": "John"}' https://api.example.com/users
http POST https://api.example.com/users name=John
# 帶 Token
curl -H "Authorization: Bearer token" https://api.example.com/users
http https://api.example.com/users Authorization:'Bearer token'| 面向 | cURL | HTTPie |
|---|---|---|
| 語法 | 較複雜 | 較直觀 |
| 顏色輸出 | 無 | 有 |
| JSON 處理 | 需手動 | 自動 |
| 安裝 | 系統內建 | 需安裝 |
| 腳本使用 | 更常見 | 較少 |
六、 腳本化
6.1 Bash 腳本
bash
#!/bin/bash
API_URL="https://api.example.com"
TOKEN="your-token"
# 登入
login() {
curl -s -X POST "$API_URL/login" \
-H "Content-Type: application/json" \
-d '{"email": "'$1'", "password": "'$2'"}' | jq -r '.token'
}
# 取得用戶
get_users() {
curl -s "$API_URL/users" \
-H "Authorization: Bearer $TOKEN" | jq
}
# 建立用戶
create_user() {
curl -s -X POST "$API_URL/users" \
-H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" \
-d @- <<EOF
{
"name": "$1",
"email": "$2"
}
EOF
}
# 使用
TOKEN=$(login "admin@example.com" "password")
get_users
create_user "John" "john@example.com"6.2 健康檢查
bash
#!/bin/bash
check_health() {
status=$(curl -s -o /dev/null -w "%{http_code}" "$1/health")
if [ "$status" = "200" ]; then
echo "✅ $1 is healthy"
else
echo "❌ $1 is down (status: $status)"
exit 1
fi
}
check_health "https://api.example.com"
check_health "https://web.example.com"總結
| 工具 | 適用場景 |
|---|---|
| cURL | 腳本、CI/CD、廣泛支援 |
| HTTPie | 互動式測試、開發除錯 |
> **記憶技巧**:
-X= 方法-H= 標頭-d= 資料-o= 輸出-v= 詳細
進階挑戰
- 使用 cURL 寫一個 API 測試腳本,包含登入、CRUD 操作。
- 比較
curl --trace和-v的輸出差異。 - 研究
xh——另一個 HTTPie 替代品。