➜ Old React website
Chung Cheuk Hang MichaelJava Web Developer
透過 Redis CLI 學習 RedisSpring Cloud Config——使用獨立既 Git 配置 repo

cURL 基本操作

Table of contents

1 cURL 簡介

cURL 係一個有好長歷史既 command line 工具,佢既主要用途係發出 HTTP 請求,可以用於測試 HTTP APIs 或者自動化等,同 Postman 既最核心功能相似,但就冇提供針對 API 測試既功能。

2 Request methods

2.1 GET

POST method 一般係用黎獲取資源,例如獲取訂單列表、獲取某一份訂單。
curl "<URL>"
註:
  • 默認係 GET method。
例子:
curl https://jsonplaceholder.typicode.com/todos/1

2.2 POST

POST method 一般係用黎新增資源(或者更新資源,視乎 API 點樣實現),例如新增訂單。
curl -d "<request body>" "<URL>"
註:
  • 因為有 -d(request body)但係冇 -X(request method),所以默認會用 POST method。
例子:
curl -d "{ "title": \"Michael's new title\" }" https://jsonplaceholder.typicode.com/todos

2.3 其他 methods

GET、POST 以外比較常見既 request methods 有:
Request method用途
PUT更新資源(整個覆蓋)
PATCH更新資源(只更新一部分)
DELETE刪除資料
OPTIONS了解資源所支持既選項,例如 request methods(Allow)、CORS(Access-Control-Allow-Methods
curl -X "<method>" "<URL>"
例子:
curl -X PUT -d "{ "title": \"Michael's new title\" }" https://jsonplaceholder.typicode.com/todos/1
curl -X PATCH -d "{ "title": \"Michael's new title\" }" https://jsonplaceholder.typicode.com/todos/1

3 Request body

通常 POSTPUTPATCH 都會伴隨 request body。
curl -d "<request body>" "<URL>"
註:
  • 如果冇 -X,默認會使用 POST method。
例子:
curl -d "{ "title": \"Michael's new title\" }" https://jsonplaceholder.typicode.com/todos

4 Request headers

常見既 request headers 有 AuthorizationContent-TypeContent-LengthAcceptCookieOrigin 等。
curl -H "<k1: v1>" -H "<k2: v2>" "<URL>"
註:
  • 當有 -d(request body)既時候,cURL 會自動計算字元長度然後加入 Content-Length header。
例子:
curl -H "Origin: https://example.com" https://jsonplaceholder.typicode.com/todos/1

4.1 Authorization:Basic Authentication

cURL 提供簡單既方法去傳入 Basic Authentication 既 username 以及 password,佢會幫我地轉化成 Base64,作為 Authorization header。
curl -u "<username>:<password>" "<URL>"
相等於自己轉化成 Base64:
var USERNAME = "Mick"; var PASSWORD = "Chung"; btoa(unescape(encodeURIComponent(USERNAME + ":" + PASSWORD))) // 'TWljazpDaHVuZw=='
然後放落 Authorization header:
curl -H "Authorization: Basic TWljazpDaHVuZw==" "<URL>"

4.2 Cookies

我地可以用 cURL 既 --cookie 或者 -b option,例如:
curl --cookie "SESSION_ID=1234" "<URL>"

5 其他選項

5.1 詳細模式

詳細模式可以顯示 response body 以外既數據,例如:
  • Request method
  • Request headers
  • Response status code
  • Response headers
如果唔太熟悉 cURL 既選項背後既邏輯會幫你發送一個點樣既請求,又或者想了解曬成個完整 response 係點樣,就一定要用詳細模式。
curl "<URL>" -v
例子:
curl https://jsonplaceholder.typicode.com/todos/1 -v

5.2 Follow redirects

如果 response status code 係 3xx(例如 301302),而 response headers 帶有 Location,咁就即係個 response 因為某啲原因想 redirect 我地去另一個 URL。
curl "<URL>" -L
例子:
第一步:先用詳細模式,留意下面 command 既執行結果:
  • Response status code 係 301 Moved Permanently
  • Response headers 帶有 Location: https://www.google.com/
  • Response body 帶有 301 MovedThe document has moved 字樣,並唔係我地期望所見到既正常 Google 網頁既 HTML 內容。
curl https://google.com -v
第二步:命令 cURL 去 follow redirects,今次 response body 先係正常既 Google 網頁既 HTML 內容。
curl https://google.com -L

5.3 跳過 SSL 檢測

呢個功能適合本地測試開發緊既網站:
curl -k https://www.google.com

6 下載檔案

curl --output-dir "<output folder path>" -o "<output file name>" "<URL>"
curl --output-dir "<output folder path>" -O "<URL>"
註:
  • --output-dir 指定下載既 folder 必須要存在。
  • 如果有 -O,就會用返個下載 URL 既檔名(唔包括任何 path)黎儲存喺本地。
    • 如果個下載既 folder 已經存在相同檔名既檔案,呢個檔案就會被覆寫。
  • 一個係細楷 -o;一個係大楷 -O,唔好混淆兩者。

7 參考資料