Linux Curl Command 指令與基本操作入門教學
前言
身為一個 Web 開發者,往往需要開發不同的 Restful API (Application Programming Interface)來存取資源。在開發完 API 後測試則會使用類似 Postman 的測試工具來進行測試。除了使用 Postman 等 GUI Tool 外,身為一個軟體工程師,當然要學會使用 Linux 指令中的 Curl!這邊我們整理了 Curl 常用的指令,讓大家可以重新溫習,讀者也可以分享自己的常用的指令來提升工作效率。好,那我們就開始吧!
Curl 指令基本介紹與常見用法
Curl 是一個在 Linux 上用來透過 HTTP Protocol(HTTP HyperText Transfer Protocol 定義存取網路資源的協定,讓我們可以使用 client / server 模式來取得網路資源)下載和上傳檔案的指令(比起 wget 只能下載強大許多)。它基本的指令格式如下:
1 | curl [options] [URL...] |
新手上路,我們來看看最基本的用法:
打開終端機(terminal)然後,curl 後面加網址,就會在終端機內顯示回傳的 response,可能是 HTML、JSON 或是 XML 等格式,根據輸入的 URL 內容而定。
1 | curl https://www.google.com |
例如我想要下載:黃色小鴨的圖片,可以使用 -o
搭配欲下載的檔名和網址
1 | curl -o duck.jpg https://im2.book.com.tw/image/getImage?i=https://www.books.com.tw/img/N00/040/56/N000405619.jpg&v=522ff1cf&w=348&h=348 |
若是使用 -O
則可以直接使用下載網址的檔案檔名來命名下載的檔案(N000405619.jpg):
1 | curl -O https://im2.book.com.tw/image/getImage?i=https://www.books.com.tw/img/N00/040/56/N000405619.jpg&v=522ff1cf&w=348&h=348 |
有可能在下載過程中被中斷,若是想要從中斷的地方繼續的話,可以使用 -C
選項:
1 | curl -C - -O http://releases.ubuntu.com/18.04/ubuntu-18.04-desktop-amd64.iso |
若希望可以跟隨著網址 301/302 redirect 的話,可以使用 -L
選項:
1 | curl -L http://google.com |
可以比較沒有使用 -L 的回應:
1 | $ curl http://google.com |
若我們要追蹤整個 curl 過程並將結果存入 debugdump.txt 檔案可以使用 --trace-ascii
指令:
1 | curl --trace-ascii debugdump.txt http://www.example.com/ |
1 | $ cat debugdump.txt |
使用 Curl 來進行 HTTP Request
除了簡易的下載檔案或是取得網頁內容外,Curl 還支援各種不同 HTTP 請求方法(HTTP method),以下列出常用指令和選項參數:
1 | -X/--request [GET|POST|PUT|DELETE|PATCH] 使用指定的 http method 來發出 http request |
- GET
簡易一個 URL 版本,可以攜帶 query string 參數取得網路資源:
1 | $ curl https://example.com?q1=123&q2=abc |
在同一個指令使用多個 URL:
1 | $ curl http://example1.com http://example2.com |
- Form POST
一般而言我們 Form 表單的 HTML 會長這樣:
1 | <form method="POST" action="form.php"> |
由於 Form post 是使用 application/x-www-form-urlencoded
Content-Type,所以傳遞的值需要編碼:
1 | $ curl -X POST --data "email=test@example.com&press=%20OK%20" http://www.example.com/form.php |
- File Upload POST
另一種常見 Form 表單是有涉及檔案上傳(使用 multipart/form-data Content-Type):
1 | <form method="POST" enctype='multipart/form-data' action="upload.php"> |
可以看到指令會需要攜帶 upload 檔案:
1 | $ curl -X POST -F 'file=@./upload.txt' http://www.example.com/upload.php |
常見 Restful CRUD 指令:
GET 單一和全部資源
1
2$ curl -X GET "http://www.example.com/api/resources"
$ curl -X GET "http://www.example.com/api/resources/1"POST JSON 資料:
1
$ curl -X POST -H "Content-Type: application/json" -d '{"status" : false, "name" : "Jack"}' "http://www.example.com/api/resources"
PUT JSON 資料:
1
$ curl -X PUT -H "Content-Type: application/json" -d '{"status" : false }' "http://www.example.com/api/resources"
DELETE 資源:
1
$ curl -X DELETE "http://www.example.com/api/resources/1"
攜帶 cookie
在指令中輸入 cookie:
1 | $ curl --cookie "name=Jack" http://www.example.com |
從檔案讀取 cookie:
1 | $ curl --cookie stored_cookies_file_path http://www.example.com |
- 攜帶 User Agent
1 | $ curl --user-agent "Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)" http://www.example.com |
- Basic Authentication
若所存取網頁有使用 Basic Authentication,可以攜帶 --user username:password
來通過驗證:
1 | $ curl -i --user secret:vary_secret http://www.example.com/api/resources |
總結
以上介紹了 Linux Curl 常用指令和基礎操作的入門教學介紹:
- GET/POST/PUT/DELETE 操作
- 下載檔案
- Form 表單操作
- 檔案上傳
- Restful CRUD 指令
- User Agent
- Basic Authentication
之後讀者除了使用 Postman 等 GUI Tool 外,也可以使用 Curl 來進行 HTTP endpoint 的測試和開發上!讀者也可以分享自己的常用的指令來提升工作效率!
關於作者:
@kdchang 文藝型開發者,夢想是做出人們想用的產品和辦一所心目中理想的學校。A Starter & Maker. JavaScript, Python & Arduino/Android lover.:)
參考文件
(image via linuxinsider)
留言討論