cURL是什麼?
c 可以看作是 client,url(Uniform Resource Locator)是統一資源定位符。
cURL 可以為指定的 url 執行網路傳輸,在 shell 和指令碼中它是非常便捷、強大、可靠的。
cURL
支援
N
多協議(ftp、smtp等等),本文只討論有關
http
基於命令列的相關話題,使用
cURL
完全可以輕而易舉地取代
postman
之流的圖形介面工具。下面看下使用
cURL
發起
http
請求。
使用
cURL
發起 http 請求
發起 http get 請求
curl http://localhost:8080/demo
使用-v 詳細顯示請求響應相關資訊
curl -v http://localhost:8080/demo
使用-G -d 發起get請求併發送資料
curl -G -d “hello” -v http://1ocalhost:8080/demo
使用-I 發起head請求
curl -I http://localhost:8080/demo
使用-i 響應包含頭部資訊
curl -i http://localhost:8080/demo
以上是基本的get請求示例,下面看下使用curl發起需要登入認證的請求。
使用 cURL 發起需要登入認證的請求
使用-u 提供使用者名稱密碼
curl -u ‘admin:admin’ http://localhost:9002/actuator
curl自動識別使用者名稱密碼
curl http://admin:admin@localhost:9002/actuator1
使用-u 僅輸入使用者名稱 會提示密碼輸入
curl -u ‘admin’ http://localhost:9002/actuator
使用-c 儲存服務端響應的cookie
curl -u ‘admin:admin’ -c cookie。txt http://localhost:9002/actuator
使用-b 攜帶cookie資訊發起http請求
curl -b cookie。txt http://localhost:9002/actuator
下面看下使用curl傳送post請求。
使用 cURL 傳送 post 請求
使用-d 傳送http post請求資料 -H指定head line頭資訊
curl -d “{‘name’:‘star’,‘age’:20}” -H “Content-type:application/json” http://localhost:8080/demo/post
使用@引用檔案 包含請求資料的檔案
curl -d @post_data -H “Content-type:application/json” http://localhost:8080/demo/post
使用-F選項 post上傳檔案
curl -F ‘fileName=@curl。png’http://localhost:8080/demo/file
使用–data-urlencode編碼 提交資料
curl ——data-urlencode ‘name=碼農小麥’ -v http://localhost:8080/demo/urlencode
使用-d 提交請求資料
curl -d ‘name=碼農小麥’ -d ‘content=歡迎來撩’ -v http://localhost:8080/demo/postcurl -d ‘name=碼農小麥&content=歡迎來撩’ -v http://localhost:8080/demo/post1
以上就是
cURL
常見的命令列使用示例,完全可以應對日常的開發測試場景,以及指令碼相關 http 請求功能實現。更多使用方法參見 curl ——help。