Curl 命令參數解析
Curl 參數:詳細解析與示例
curl 是一個功能強大的命令行工具,用于傳輸數據。它支持多種協議,如 HTTP、HTTPS、FTP、SFTP 等。curl 提供了豐富的參數,以滿足各種傳輸需求。本文將詳細解析 curl 參數,并通過代碼示例說明其用法。
1. 參數概述
curl 參數分為兩大類:通用參數和協議相關參數。通用參數適用于所有協議,而協議相關參數僅適用于特定協議。以下是一些常用的 curl 參數:
-h, --help:顯示幫助信息。-v, --verbose:顯示詳細的信息,包括請求和響應頭。-d, --data:發送 POST 請求時,用于發送數據。-H, --header:添加自定義請求頭。-L, --location:跟隨重定向。-o, --output:將響應內容保存到文件中。
2. 通用參數
2.1 -h, --help
顯示 curl 的幫助信息。例如:
curl -h
2.2 -v, --verbose
顯示詳細的信息,包括請求和響應頭。例如:
curl -v https://www.example.com
2.3 -d, --data
發送 POST 請求時,用于發送數據。例如,向一個 REST API 發送 JSON 數據:
curl -d '{"name": "John Doe", "age": 30}' https://api.example.com/users
2.4 -H, --header
添加自定義請求頭。例如,發送帶有 Authorization 頭部的請求:
curl -H "Authorization: Bearer 123456" https://api.example.com/users
2.5 -L, --location
跟隨重定向。例如,獲取一個經過重定向的 URL 的內容:
curl -L https://www.example.com
2.6 -o, --output
將響應內容保存到文件中。例如,將一個網頁保存為 HTML 文件:
curl -o index.html https://www.example.com
3. 協議相關參數
curl 支持多種協議,每個協議都有其特定的參數。以下是一些常見的協議參數:
- HTTP:
-A, --user-agent、-b, --cookie、-c, --cookie-jar、-C, --continue-at - HTTPS:
-k, --insecure、-I, --head - FTP:
-u, --user、-p, --password、-P, --port、-s, --ssl - SFTP:
-s, --ssl、-P, --port、-u, --user
4. 示例
以下是一個使用 curl 發送 GET 請求并獲取響應內容的示例:
curl https://www.example.com
以下是一個使用 curl 發送 POST 請求并獲取響應內容的示例:
curl -XPOST -H 'Content-Type: application/json' -d '{"name":"example"}' https://www.example.com/
以下是一個使用 curl 發送帶有自定義請求頭的 GET 請求的示例:
curl -H "Authorization: Bearer 123456" https://api.example.com/users
發送一個帶有HTTP Basic認證的GET請求:
curl -u username:password https://www.example.com/
發送一個PUT請求,同時指定請求頭和請求體:
curl -XPUT -H 'Content-Type: application/json' -d '{"name":"example"}' https://www.example.com/

浙公網安備 33010602011771號