Skip to main content

cURL: Tips

Options in the integration with Shell
CURL_CMD="curl"
CURL_OPT="-qSfs -m 60"
CURL_OPT_POST="-qSfs -m 60 -X POST"
POST Request

Send an Empty POST Request

curl -X POST https://catonmat.net

Send a POST Request with Form Data

curl -d 'login=emma&password=123' -X POST https://google.com/login
GET Request

Send a GET Request and Print the Response to Screen

curl https://catonmat.net/api?a=1&b=2

Send a GET Request and Save the Response to a File

curl -o response.txt https://catonmat.net/api?a=1&b=2
Self-Signed Certificate

Error: curl: (60) SSL certificate problem: unable to get local issuer certificate

用 curl 連線 HTTPS 網站時,如遇到 self-signed certificate 類型或有些網站憑證,在瀏覽器上顯示是安全;在 curl 會出現錯誤訊息。原因是網站的簽發者憑證不受用戶端 curl 的信任。 

如何解決信任問題:

  1. 用 Firefox 或 Chrome 下載目的網站的簽發者 CA 憑證 *.pem
  2. curl 指定 *.pem  CA 憑證連接網站

curl 要使用特定 CA 憑證檔 *.pem,方法有二:

# 方法一
curl --cacert your-server-ca.pem https://your-server-name

# 方法二
# 先找出 curl 的 CApath, 隨便設定一個錯誤的 CA file
curl --cacert XXX https://your-server-name

curl: (77) error setting certificate verify locations:
  CAfile: xxx
  CApath: /etc/ssl/certs  <===

# 在 CApath 裡有一個檔案 ca-certificates.crt , 這用來信任那些客製的憑證
# 從檔案最底端加上新的憑證
# 加入的憑證一旦信任成功,curl 就不再需要指定 CA file
cat your-server-ca.pem >> /etc/ssl/certs/ca-certificates.crt
curl https://your-server-name
HTTP Status Codes

http_status_codes.jpg

Use a Proxy
curl -x username:Password@myproxy.com:8080 https://catonmat.net
Cheat Sheet

curl-cheat-sheet.png