# cURL

cURL是一個開源專案，主要的產品是curl和libcurl，兩者功能均是：基於網路協定，對指定URL進行網路傳輸。 cURL涉及是任何網路協定傳輸，不涉及對具體資料的具體處理。

# Tips

##### Cheat Sheet

[![curl-cheat-sheet.png](https://osslab.tw/uploads/images/gallery/2022-09/scaled-1680-/curl-cheat-sheet.png)](https://osslab.tw/uploads/images/gallery/2022-09/curl-cheat-sheet.png)

##### 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](https://osslab.tw/uploads/images/gallery/2022-08/scaled-1680-/http-status-codes.jpg)](https://osslab.tw/uploads/images/gallery/2022-08/http-status-codes.jpg)

##### Use a Proxy  


```shell
curl -x username:Password@myproxy.com:8080 https://catonmat.net
```

##### Download

```bash
curl -L https://github.com/path/to/the/release/file > releasefile

curl --request GET \
  --url 'https://www.tenable.com/downloads/api/v2/pages/nessus/files/Nessus-10.5.1-es9.x86_64.rpm' \
  --output 'Nessus-10.5.1-es9.x86_64.rpm'
```

# Online Tools

More online tools

- [Online CSV Tools](https://onlinecsvtools.com/)
- [Online Time Tools](https://onlinetimetools.com/)

##### Public IP

Web Online

- [https://ipinfo.io/what-is-my-ip](https://ipinfo.io/what-is-my-ip)

```
↪ curl ifconfig.me
219.68.222.111⏎   
↪ curl ifconfig.me/ip
219.68.222.111⏎

↪ curl ip.im
↪ curl ipv4.im
↪ curl ip.im/info
↪ curl ip.im/8.8.8.8

curl ifconfig.co
curl checkip.amazonaws.com
curl icanhazip.com
curl ipecho.net/plain
curl -Ls myip.check.place
```

##### CryptoCurrency

[rate.sx](http://rate.sx/)

[![curl-rate-sx.png](http://www.osslab.tw/uploads/images/gallery/2020-12/scaled-1680-/curl-rate-sx.png)](http://www.osslab.tw/uploads/images/gallery/2020-12/curl-rate-sx.png)

```shell
curl rate.sx
curl rate.sx/btc
curl rate.sx/btc@3d  # for last 3 days
curl rate.sx/1btc    # convert 1 BTC to ?? USD
curl rate.sx/:help
```

##### Weather

[wttr.in](http://wttr.in/v2.wttr.in)

[![curl-wttr-in.png](http://www.osslab.tw/uploads/images/gallery/2020-12/scaled-1680-/curl-wttr-in.png)](http://www.osslab.tw/uploads/images/gallery/2020-12/curl-wttr-in.png)

```shell
curl wttr.in
curl wttr.in/TaoYuan
curl wttr.in/:help
```

##### QRcode

[qrenco.de](http://qrenco.de/)

[![curl-qrenco-de.png](http://www.osslab.tw/uploads/images/gallery/2020-12/scaled-1680-/curl-qrenco-de.png)](http://www.osslab.tw/uploads/images/gallery/2020-12/curl-qrenco-de.png)

```shell
curl qrenco.de
curl qrenco.de/www.yutube.com
curl qrenco.de/:help
printf "$@" | curl -F-=\<- qrenco.de
```

##### Shorten URL

[0x0.st](http://0x0.st/)

[![curl-0x0-st.png](http://www.osslab.tw/uploads/images/gallery/2020-12/scaled-1680-/curl-0x0-st.png)](http://www.osslab.tw/uploads/images/gallery/2020-12/curl-0x0-st.png)

```shell
curl -F 'shorten=https://www.osslab.tw' https://0x0.st
```

Tinyurl.com: short URL from the terminal

```shell
echo https://URL | xargs -r -I {} curl -s -d url={} -G https://tinyurl{.}com/create.php | \
 awk '/indent/ {print substr($2,19,39)}' | sed '2!d' | sed 's/<.*//'
```

# Learning cURL

- [Basics of HTTP Requests with cURL: An In-Depth Tutorial](https://bytexd.com/basics-http-requests-curl-tutorial/)
- [Curl Cookbook](https://catonmat.net/cookbooks/curl)
- [Httpie](https://httpie.io/) - Alternative to Curl
- [posting](https://github.com/darrenburns/posting) - A powerful HTTP client that lives in your terminal.