curl 是一個強大的命令行工具,用于在 Linux 系統(tǒng)中進行數(shù)據(jù)傳輸。它支持多種協(xié)議,包括 HTTP、HTTPS、FTP 等,用于下載或上傳數(shù)據(jù),執(zhí)行 Web 請求等。

curl 命令的常見用法和解釋:
1. 發(fā)送 HTTP GET 請求:
curl http://example.com
這會向指定的 URL 發(fā)送 HTTP GET 請求并將響應輸出到終端。
2. 發(fā)送 HTTP POST 請求:
curl -X POST -d?“param1=value1?m2=value2”?http://example.com
通過 -X POST 指定請求方法,-d 選項用于傳遞 POST 數(shù)據(jù)。
3. 下載文件:
curl -O http://example.com/file.zip
使用 -O 選項將遠程文件下載到本地,文件名與遠程文件相同。
4. 上傳文件:
curl -F?“file=@localfile.txt”?http://example.com/upload
使用 -F 選項將本地文件上傳到指定 URL。
5. 自定義請求頭:
curl -H?“Authorization: Bearer token”?http://example.com/api/resource
使用 -H 選項可以設(shè)置自定義請求頭。
6. 使用代理:
curl -x proxy_server:port http://example.com
-x 選項可用于指定代理服務(wù)器。
7. 保存響應到文件:
curl -o output.txt http://example.com
使用 -o 選項將響應保存到本地文件 output.txt。
8. 鏈接超時和最大執(zhí)行時間:
curl –connect-timeout 10 –max-time 60 http://example.com
使用 –connect-timeout 設(shè)置連接超時時間,–max-time 設(shè)置最大執(zhí)行時間。
9. 使用基本認證:
curl -u username:password http://example.com
使用 -u 選項進行 HTTP 基本認證。
10. 下載文件并顯示下載進度:
curl -# -O http://example.com/largefile.zip
-# 選項會顯示下載進度條。

11. 傳遞 JSON 數(shù)據(jù):
curl -H?“Content-Type: application/json”?-d?‘{“key”: “value”}’?http://example.com/api/resource
使用 -H 設(shè)置請求頭為 JSON 類型,-d 選項傳遞 JSON 數(shù)據(jù)。
12. 限速下載速度:
curl –limit-rate 100k -O http://example.com/largefile.zip
使用 –limit-rate 選項限制下載速度,此示例限制下載速度為100KB/s。
13. 重試連接:
curl –retry 3 http://example.com
使用 –retry 選項在連接失敗時重試請求,此示例最多重試3次。
14. 下載文件時顯示文件大?。?/strong>
curl -I -s -X HEAD http://example.com/largefile.zip | grep Content-Length | awk?‘{print $2}’
使用 -I 發(fā)送 HTTP HEAD 請求以獲取文件大小。
15. 處理重定向:
curl -L http://example.com
使用 -L 選項自動處理HTTP重定向。
16. 批量下載:
curl -O http://example.com/{file1,file2,file3}.txt
使用大括號擴展來批量下載多個文件。
17. 使用Cookie:
curl –cookie?“session=abcdef123456”?http://example.com
使用 –cookie 選項發(fā)送Cookie。
18. 發(fā)送表單數(shù)據(jù):
curl -d?“username=user&password=pass”?-X POST http://example.com/login
使用 -d 發(fā)送表單數(shù)據(jù)。
19. 查看HTTP請求頭:
curl -I http://example.com
使用 -I 查看HTTP響應頭。
20. 使用多線程下載:
curl -O http://example.com/largefile.zip –parallel
使用 –parallel 啟用多線程下載。
21. 使用代理服務(wù)器:
curl -x http://proxyserver:port http://example.com
使用 -x 選項指定代理服務(wù)器地址和端口。
22. 上傳文件:
curl -F?“file=@localfile.txt”?http://example.com/upload
使用 -F 選項上傳本地文件到遠程服務(wù)器。
23. 自定義請求方法:
curl -X PUT http://example.com/resource
使用 -X 選項指定自定義HTTP請求方法,如PUT、DELETE等。
24. HTTP Basic認證:
curl -u username:password http://example.com
使用 -u 選項進行HTTP基本認證。
25. 顯示HTTP請求和響應信息:
curl -v http://example.com
使用 -v 選項顯示HTTP請求和響應的詳細信息。
26. 保存響應到文件:
curl -o output.html http://example.com
使用 -o 選項將HTTP響應保存到本地文件。
27. 帶有HTTP Header的請求:
curl -H?“Accept: application/json”?-H?“Authorization: Bearer token”?http://example.com/api/resource
使用多個 -H 選項添加自定義HTTP頭。
28. 跳過SSL證書驗證:
curl -k https://example.com
使用 -k 選項跳過SSL證書驗證。
29. 使用socks5代理:
curl –socks5 host:port http://example.com
使用 –socks5 選項連接到socks5代理服務(wù)器。
30. 使用連接超時:
curl –connect-timeout 10 http://example.com
使用 –connect-timeout 選項設(shè)置連接超時時間。
31. 限制最大重定向次數(shù):
curl –max-redirs 3 http://example.com
使用 –max-redirs 選項限制最大重定向次數(shù)。

版權(quán)聲明:本文內(nèi)容由互聯(lián)網(wǎng)用戶自發(fā)貢獻,該文觀點僅代表作者本人。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔相關(guān)法律責任。如發(fā)現(xiàn)本站有涉嫌抄襲侵權(quán)/違法違規(guī)的內(nèi)容, 請發(fā)送郵件至2705686032@qq.com 舉報,一經(jīng)查實,本站將立刻刪除。原文轉(zhuǎn)載: 原文出處: