cURL


這篇主要是整理過去經常使用到的範例,同時也是作為教育訓練使用的文件。以下的範例都是在 Linux or Mac OS 執行的。

大綱

  1. Basic usage: 基本的用法與常用的參數 (Options)
  2. HTML form
  3. Cookie
  4. HTTP authencation
  5. RESTful
  6. Debugging and Scripting

Basic usage

基本的格式和一般 CLI (Commnad-line interface) 都一樣。

1
Usage: curl [options...] <url>

Basic options

參考 man curl 以及 curl --help 內容,摘錄幾個基本參數。

  1. -o, --output FILE: Write output to file instead of stdout
  2. -S, --show-error: Show error. With -s, make curl show errors when they occur
  3. -s, --silent: Silent mode. Don’t output anything
  4. -A, --user-agent STRING: User-Agent to send to server (H)
  5. -v, --verbose: Make the operation more talkative
  6. -V, --version: Show version number and quit
  7. -w, --write-out FORMAT: What to output after completion
  8. -O/--remote-name: Write output to a file named as the remote file

Examples (常見例子)

  1. GET a http content: curl www.google.com
  2. Download a file: curl -O <url>
  3. Download a file, and specify file name: curl -o <filename> <url>

類似的工具: wget <url>

HTML form

常用的 options:

  1. -d/--data <data>: HTTP POST data (H)
  2. --data-ascii <data>: HTTP POST ASCII data (H)
  3. --data-binary <data>: HTTP POST binary data (H)

簡單的例子:

1
2
3
PARAMS="username=rickh&password=passw0rd"
URL=http://127.0.0.1/login
curl -d "${PARAMS}" $URL

POST JSON Data

下面另一個 POST JSON data 的例子, 注意 -d 引號的格式, 資料內容如果有雙引號, 那麼用單引號描述資料內容, 如果使用雙引號描述 -d, 那麼資料內容的引號需要被 escape, 如下:

POST JSON data
1
2
3
URL=http://127.0.0.1/login
curl -d '{"enable": 1}' ${URL}
curl -d "{\"enable\": 1}" ${URL}

如果 JSON 本身是存在另一個檔案, 那麼可以用小老鼠指定檔名, 這在測試 RESTful 時很好用, 如下:

1
curl -d @data.json ${URL}

有時候需要模擬瀏覽器的行為, 特別是有 cookie 的網站. 可以利用參數 -c 將存起來檔案, 然後在下一個動作利用 -b 指定 cookie 檔案.

  1. -c, --cookie-jar FILE: Write cookies to this file after operation (H)
  2. -b, --cookie STRING/FILE: String or file to read cookies from (H)

延續前一個段落的例子:

1
2
3
URL=http://127.0.0.1/login
curl -c cookies.txt -d '{"enable": 1}' ${URL}
curl -b cookies.txt ${URL}

以下是一個 cookie 的內容範例:

1
10.1.100.82 FALSE / FALSE 0 admin:language  zh-cn

HTTP authencation

幾個常用到的 options:

  1. -u/--user <user[:password]>: Set server user and password
  2. -k/--insecure: Allow connections to SSL sites without certs (H)
HTTP authencation
1
2
3
4
ACCOUNT=rickh
PASSWORD=passw0rd
URL=http://192.168.1.1/login
curl --user "$ACCOUNT:$PASSWORD" ${URL}

如果是 HTTPS, 那麼可以帶上 -k, 變成: curl -k --user "$ACCOUNT:$PASSWORD" ${URL}

RESTful client

RESTful 利用 HTTP protocol 特性為基礎,所以只要能夠指定相關參數即可,像是 HTTP Header, HTTP Method, Data, 還有如何存下接收的 Data, 相關參數:

  1. -H/--header <line>: Custom header to pass to server (H)
  2. -X, --request COMMAND: Specify request command to use, GET/POST/PUT/DELETE

以下是一個指定 Content-Type 和 Accept 為 application/json 的例子,要留意多個 HTTP Header 使用多個 -H 描述:

RESTful client: PUT
1
2
3
4
api_url=http://10.1.101.204:8080/api/app/login
api_header='-H "Accept: application/json" -H "Content-Type: application/json"'

curl ${api_header} -X PUT --data @login.json ${api_url}

Debugging and Scripting

使用 curl 有時候需要更多細節用來 debug, 但是用在程式裡當作功能使用時, 可能會希望不要有太多額外的訊息, 或者隱藏這些資料. 所以前述的例子, 可以搭配以下的參數作為 debug or script 使用:

  1. -D, --dump-header FILE: Write the headers to this file
  2. -f, --fail: Fail silently (no output at all) on HTTP errors (H)
  3. -i, --include: Include protocol headers in the output (H/F)
  4. -o, --output FILE: Write output to file instead of stdou
  5. -s, --silent: Silent mode. Don’t output anything
  6. -v, --verbose: Make the operation more talkative
  7. -w, --write-out FORMAT: What to output after completion

延伸閱讀

站內資料

參考資料



Comments

  • 全站索引
  • 關於這裏
  • 關於作者
  • 學習法則
  • 思考本質
  • 一些領悟
  • 分類哲學
  • ▲ TOP ▲