Linux之curl命令的使用及說(shuō)明
一. 前期準(zhǔn)備
1.1 curl命令簡(jiǎn)介
curl 是一個(gè)強(qiáng)大的命令行工具,用于向服務(wù)器發(fā)送請(qǐng)求并獲取數(shù)據(jù)。
它支持多種協(xié)議(如 HTTP、HTTPS、FTP、SFTP 等),常用于接口測(cè)試、下載文件、提交表單等操作。
命令行參數(shù)多達(dá)幾十種。如果熟練的話,完全可以取代 Postman 這一類的圖形界面工具。
除了curl命令之外,類似的還有HTTPie命令。
- 官網(wǎng):
https://httpie.io - Github:
https://github.com/httpie/cli
| 項(xiàng)目 | curl | HTTPie |
|---|---|---|
| 命令名 | curl | http |
| 默認(rèn)預(yù)裝 | ? Linux 預(yù)裝 | ? 需手動(dòng)安裝 |
| 所屬語(yǔ)言 | C 編寫(高性能) | Python 編寫(易擴(kuò)展) |
| 依賴情況 | 幾乎無(wú)依賴 | 依賴 Python 環(huán)境 |
| 支持協(xié)議 | HTTP, HTTPS, FTP, SFTP, SCP, SMTP 等 | 僅支持 HTTP 和 HTTPS |
1.2 環(huán)境準(zhǔn)備
截止到2025年7月31日,curl的最新版本為8.15.0,由于linux系統(tǒng)預(yù)裝的并不是最新版本的curl命令,此處介紹下載curl源碼的方式,編譯安裝。筆者使用的是如下版本的Ubuntu系統(tǒng)。
apluser@FengYeHong-HP:work$ cat /etc/os-release PRETTY_NAME="Ubuntu 22.04.1 LTS" NAME="Ubuntu" VERSION_ID="22.04" VERSION="22.04.1 LTS (Jammy Jellyfish)" VERSION_CODENAME=jammy ID=ubuntu ID_LIKE=debian HOME_URL="https://www.ubuntu.com/" SUPPORT_URL="https://help.ubuntu.com/" BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/" PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy" UBUNTU_CODENAME=jammy
1.2.1 安裝源碼編譯依賴庫(kù)
sudo apt update sudo apt install -y build-essential libssl-dev libnghttp2-dev libz-dev libidn2-dev libpsl-dev
| 庫(kù)名 | 作用 | 用途 |
|---|---|---|
| build-essential | 安裝構(gòu)建 C/C++ 程序所需的基礎(chǔ)工具, 包含了 gcc, g++, make 以及其他常用構(gòu)建工具。 | 幾乎所有需要編譯源代碼的軟件都會(huì)依賴它。 |
| libssl-dev | OpenSSL 的開發(fā)庫(kù)。 | 支持使用 HTTPS, TLS/SSL 等安全協(xié)議的程序開發(fā), 比如 curl、wget、openssl 自己等。 |
| libnghttp2-dev | HTTP/2 協(xié)議支持庫(kù)的開發(fā)包。 | 為支持 HTTP/2 的網(wǎng)絡(luò)程序提供功能, 如新版 curl、nginx、httpie 等。 |
| libz-dev | zlib 的開發(fā)包(壓縮庫(kù))。 | 支持 gzip/deflate 等壓縮算法, 是許多程序處理壓縮數(shù)據(jù)(如 HTTP 請(qǐng)求)時(shí)所需的庫(kù)。 |
| libidn2-dev | 用于國(guó)際化域名(IDN)的解析(支持非 ASCII 域名)。 | 使網(wǎng)絡(luò)工具能識(shí)別形如 https://你好.中國(guó) 這種 URL。 |
| libpsl-dev | Public Suffix List 的開發(fā)包。 | 在處理域名時(shí)幫助判斷頂級(jí)域(如 .com.cn 是一個(gè)后綴),用于 cookie 安全處理等。 |
1.2.2 下載并編譯安裝最新的curl
下載最新的curl安裝包
- 最新的版本號(hào),去curl的官網(wǎng)獲取
echo "8.15.0" | xargs -I {} curl -LO https://curl.se/download/curl-{}.tar.gz
tar -xzf curl-8.7.1.tar.gz cd curl-8.7.1 ./configure --with-ssl make -j$(nproc) sudo make install
1.2.3 版本確認(rèn)
編譯安裝之后,確認(rèn)curl命令的最新版本,然而此時(shí)的版本顯示為舊版本
apluser@FengYeHong-HP:curl-8.15.0$ curl --version curl 7.81.0 (x86_64-pc-linux-gnu) libcurl/7.81.0 OpenSSL/3.0.2 zlib/1.2.11 brotli/1.0.9 zstd/1.4.8 libidn2/2.3.2 libpsl/0.21.0 (+libidn2/2.3.2) libssh/0.9.6/openssl/zlib nghttp2/1.43.0 librtmp/2.3 OpenLDAP/2.5.13 Release-Date: 2022-01-05 Protocols: dict file ftp ftps gopher gophers http https imap imaps ldap ldaps mqtt pop3 pop3s rtmp rtsp scp sftp smb smbs smtp smtps telnet tftp Features: alt-svc AsynchDNS brotli GSS-API HSTS HTTP2 HTTPS-proxy IDN IPv6 Kerberos Largefile libz NTLM NTLM_WB PSL SPNEGO SSL TLS-SRP UnixSockets zstd apluser@FengYeHong-HP:curl-8.15.0$
編譯安裝的 curl 默認(rèn)路徑是 /usr/local/bin/curl,可能和系統(tǒng)原本/usr/bin/curl沖突,你可以通過(guò)以下方式優(yōu)先使用新的版本
# 清除舊路徑緩存 apluser@FengYeHong-HP:curl-8.15.0$ hash -r # 查看curl命令的最新路徑 apluser@FengYeHong-HP:curl-8.15.0$ which curl /usr/local/bin/curl
再次確認(rèn)curl版本,會(huì)發(fā)現(xiàn)提示報(bào)錯(cuò),出現(xiàn)此錯(cuò)誤的原因是
- curl 可執(zhí)行文件在運(yùn)行時(shí)找不到(或找到了錯(cuò)誤版本的)共享庫(kù)(libcurl) 中的
curl_easy_header符號(hào) - 該函數(shù)是從
libcurl 8.x開始才引入的,而你現(xiàn)在的環(huán)境可能仍然鏈接到了舊版的libcurl.so.4(來(lái)自 7.x)
apluser@FengYeHong-HP:curl-8.15.0$ curl --version curl: symbol lookup error: curl: undefined symbol: curl_easy_header apluser@FengYeHong-HP:curl-8.15.0$
1.2.4 添加動(dòng)態(tài)庫(kù)鏈接路徑
確保系統(tǒng)永久識(shí)別新版本 libcurl
# 1. 確認(rèn)新版本 libcurl 已安裝到 /usr/local/lib ls /usr/local/lib | grep libcurl # 2. 添加 /usr/local/lib 到動(dòng)態(tài)庫(kù)搜索路徑 echo "/usr/local/lib" | sudo tee /etc/ld.so.conf.d/local-curl.conf # 3. 讓系統(tǒng)重新加載動(dòng)態(tài)鏈接器緩存 sudo ldconfig
然后就可以發(fā)現(xiàn),curl的版本為最新的了
apluser@FengYeHong-HP:~$ which curl /usr/local/bin/curl apluser@FengYeHong-HP:~$ curl --version curl 8.15.0 (x86_64-pc-linux-gnu) libcurl/8.15.0 OpenSSL/3.0.2 zlib/1.2.11 libidn2/2.3.2 libpsl/0.21.0 nghttp2/1.43.0 Release-Date: 2025-07-16 Protocols: dict file ftp ftps gopher gophers http https imap imaps ipfs ipns mqtt pop3 pop3s rtsp smb smbs smtp smtps telnet tftp ws wss Features: alt-svc AsynchDNS HSTS HTTP2 HTTPS-proxy IDN IPv6 Largefile libz NTLM PSL SSL threadsafe TLS-SRP UnixSockets
1.3 Springboot接口準(zhǔn)備
1.3.1 get接口
@GetMapping("/get")
@ResponseBody
public ResponseEntity<Map> getCurl(
@RequestParam(value = "JobId", required = false) String jobId
, @RequestParam(value = "INFILE", required = false) String inFilePath
, @RequestParam(value = "OTFILE", required = false) String outFilePath
, @RequestParam(value = "name", required = false) String name
) {
Map<Object, Object> map = new HashMap<>() {{
put("id", 110120);
put("name", "楓葉紅");
}};
return ResponseEntity.ok(map);
}
1.3.2 Post接口
1.3.2.1 from
@Data
public class Test36Form {
private String order_id;
private String event;
}
@PostMapping("/form/post")
public ResponseEntity<Map<String, String>> formPostCurl(Test36Form curlForm) {
System.out.println(curlForm);
Map<String, String> map = new HashMap<>() {{
put("type", "form請(qǐng)求");
put("result", "成功");
}};
return ResponseEntity.ok(map);
}
1.3.2.2 json
@PostMapping("/json/post")
public ResponseEntity<Map<String, String>> jsonPostCurl(
@RequestHeader(value = "X-TOKEN", required = false) String xToken,
@RequestBody Map<String, Object> mapData
) {
System.out.println(mapData);
System.out.println("接收到的token請(qǐng)求頭為: " + xToken);
Map<String, String> map = new HashMap<>() {{
put("type", "json請(qǐng)求");
put("result", "成功");
}};
return ResponseEntity.ok(map);
}
1.3.3 文件上傳
1.3.3.1 表單提交
@PostMapping("/file/upload")
public ResponseEntity<Map<String, String>> fileUploadCurl(MultipartHttpServletRequest request) {
// 獲取curl命令上傳的文件
MultiValueMap<String, MultipartFile> multiFileMap = request.getMultiFileMap();
List<MultipartFile> file1 = multiFileMap.get("file1");
// 獲取上傳的文件名稱
System.out.println(file1.get(0).getOriginalFilename());
// 獲取curl文提交的數(shù)據(jù)
Map<String, String[]> parameterMap = request.getParameterMap();
String[] params1 = parameterMap.get("jsonData1");
System.out.println(Arrays.toString(params1));
String[] params2 = parameterMap.get("jsonData2");
System.out.println(Arrays.toString(params2));
Map<String, String> map = new HashMap<>() {{
put("type", "文件上傳請(qǐng)求");
put("result", "成功");
}};
return ResponseEntity.ok(map);
}
1.3.3.2 Put文件上傳
@PutMapping("/put/file")
public ResponseEntity<Map<String, String>> putFile(HttpServletRequest request) throws IOException {
// 獲取請(qǐng)求頭中的包含的上傳文件名稱
String fileName = request.getHeader("X-Filename");
System.out.println("上傳的文件名稱為: " + fileName);
// 獲取上傳的文件輸入流
InputStream inputStream = request.getInputStream();
// 文件保存
Path filePath = Paths.get(fileName);
Files.copy(inputStream, filePath, StandardCopyOption.REPLACE_EXISTING);
Map<String, String> map = new HashMap<>() {{
put("type", "文件上傳請(qǐng)求");
put("result", "成功");
}};
return ResponseEntity.ok(map);
}
二. 常用參數(shù)
2.1 請(qǐng)求方式相關(guān)
2.1.1-G⇔--get發(fā)送Get請(qǐng)求
發(fā)送Get請(qǐng)求
apluser@FengYeHong-HP:~$ curl -G -w '\n' 'http://192.168.3.24:9002/curl/get'
{"name":"楓葉紅","id":110120}
apluser@FengYeHong-HP:~$
apluser@FengYeHong-HP:~$ curl --get -w '\n' 'http://192.168.3.24:9002/curl/get'
{"name":"楓葉紅","id":110120}
apluser@FengYeHong-HP:~$
apluser@FengYeHong-HP:~$ curl -X GET -w '\n' 'http://192.168.3.24:9002/curl/get'
{"name":"楓葉紅","id":110120}
2.1.2-X⇔--request指定http請(qǐng)求方式
發(fā)送Get請(qǐng)求
apluser@FengYeHong-HP:~$ curl -X GET -w '\n' 'http://192.168.3.24:9002/curl/get'
{"name":"楓葉紅","id":110120}
發(fā)送Post請(qǐng)求
-w '\n':為了讓curl文和返回的json中間有個(gè)空行--header 'Content-Type: application/json':聲明發(fā)送的json數(shù)據(jù)
apluser@FengYeHong-HP:~$ echo;curl -X POST -w '\n' \
--header 'Content-Type: application/json' \
--data-binary '{"order_id": "order134"}' \
'http://192.168.3.24:9002/curl/json/post'
{"result":"成功","type":"json請(qǐng)求"}
由于提交的是json數(shù)據(jù),curl會(huì)自動(dòng)推測(cè)是出post命令,-X POST可以省略
- 注意:由于json協(xié)議默認(rèn)就是
UTF-8,因此不需要通過(guò)charset特別指定編碼
apluser@FengYeHong-HP:~$ curl -v -w '\n' \
--header 'Content-Type: application/json' \
--data-binary '{"order_id": "order134"}' \
'http://192.168.3.24:9002/curl/json/post'
* Trying 192.168.3.24:9002...
* Connected to 192.168.3.24 (192.168.3.24) port 9002
* using HTTP/1.x
> POST /curl/json/post HTTP/1.1
> Host: 192.168.3.24:9002
> User-Agent: curl/8.15.0
> Accept: */*
> Content-Type: application/json
> Content-Length: 24
>
* upload completely sent off: 24 bytes
< HTTP/1.1 200
< Content-Type: application/json
< Transfer-Encoding: chunked
< Date: Fri, 01 Aug 2025 23:42:29 GMT
<
* Connection #0 to host 192.168.3.24 left intact
{"result":"成功","type":"json請(qǐng)求"}
2.2 數(shù)據(jù)提交方式相關(guān)
2.2.1-d⇔--data和--data-binary
| 選項(xiàng) | 全稱 | 特點(diǎn) |
|---|---|---|
| -d | --data | 默認(rèn)以 application/x-www-form-urlencoded 發(fā)送;可多次使用 |
| --data | --data | 等同于 -d,更長(zhǎng)的寫法 |
| --data-binary | --data-binary | 不會(huì)處理數(shù)據(jù)(如不轉(zhuǎn)義換行等);適用于發(fā)送原始二進(jìn)制,JSON,XML數(shù)據(jù) |
-d / --data
- 會(huì)對(duì)傳入的數(shù)據(jù)進(jìn)行 URL 編碼處理,如果沒有手動(dòng)指定
Content-Type: application/json,它會(huì)自動(dòng)使用application/x-www-form-urlencoded - 若傳入多段數(shù)據(jù),會(huì)自動(dòng)合并為一個(gè) POST 請(qǐng)求體
- 除非顯式指定其他方法,否則會(huì)把請(qǐng)求方法改為 POST,因此如果使用了
-d / --data的話,-X POST可省略
--data-binary
- 不會(huì)對(duì)數(shù)據(jù)進(jìn)行任何處理(保持原樣發(fā)送,包括換行符、空格等)。
- 更適合用于發(fā)送 JSON、XML、文件內(nèi)容等原始結(jié)構(gòu)。
- 也會(huì)把請(qǐng)求方法改為 POST,但不會(huì)對(duì)你輸入的數(shù)據(jù)進(jìn)行預(yù)處理。
# ?-d
curl -v -w '\n' \
--header 'Content-Type: application/json' \
-d '{"order_id": "order134"}' \
'http://192.168.3.24:9002/curl/json/post'
# ?--data
curl -v -w '\n' \
--header 'Content-Type: application/json' \
--data '{"order_id": "order134"}' \
'http://192.168.3.24:9002/curl/json/post'
# ?--data-binary
curl -v -w '\n' \
--header 'Content-Type: application/json' \
--data-binary '{"order_id": "order134"}' \
'http://192.168.3.24:9002/curl/json/post'
- 以上三種請(qǐng)求,得到的結(jié)果均如下圖所示

但如果省略了--header 'Content-Type: application/json'的請(qǐng)求頭之后,使用-d / --data的話,就會(huì)默認(rèn)以 application/x-www-form-urlencoded的表單方式提交數(shù)據(jù)。
curl -v -w '\n' \ --data 'order_id=order134' \ --data 'event=PLACED' \ 'http://192.168.3.24:9002/curl/form/post'
- 效果如下圖所示

2.2.2--json
2022年的7.82.0版本的curl追加了--json配置項(xiàng),專門用來(lái)發(fā)送json數(shù)據(jù)。
7.82.0版本的之前的curl必須指定Content-Type: application/json的head才能使用。
curl -v -w '\n' \
--header 'Content-Type: application/json' \
--data-binary '{"order_id": "order134"}' \
'http://192.168.3.24:9002/curl/json/post'
7.82.0版本的之后的curl,直接使用--json配置項(xiàng)即可發(fā)送json數(shù)據(jù)。
curl -v -w '\n' \
--json '{"order_id": "order134"}' \
'http://192.168.3.24:9002/curl/json/post'

- 支持流的方式使用json數(shù)據(jù)
apluser@FengYeHong-HP:~$ echo '{"product_id": 20.5}' | \
curl -w '\n' \
--json @- \
--location 'http://192.168.3.24:9002/curl/json/post'
{"result":"成功","type":"json請(qǐng)求"}
- 支持文件的方式使用json數(shù)據(jù)
apluser@FengYeHong-HP:work$ cat file.json
{"product_id": 20.5, "name": "楓葉紅"}
apluser@FengYeHong-HP:work$
apluser@FengYeHong-HP:work$ curl -w '\n' \
--json @file.json \
--location 'http://192.168.3.24:9002/curl/json/post'
{"result":"成功","type":"json請(qǐng)求"}
2.2.3-F⇔--form
-F 或 --form 表示使用 multipart/form-data 類型的表單提交數(shù)據(jù)
- 用于上傳文件
- 用于上傳文件的同時(shí),提交json數(shù)據(jù)等
-F和-d不能同時(shí)使用
curl -v -w '\n' \ --form 'file1=@grep_cmd.txt' \ 'http://192.168.3.24:9002/curl/file/upload'

上傳文件的同時(shí)提交json數(shù)據(jù)
curl -v -w '\n' \
--form 'file1=@grep_cmd.txt' \
--form 'jsonData1={"name":"wangwu","age":30,"card":["abc","bbc"]}' \
--form 'jsonData1=測(cè)試信息' \
--form 'jsonData1=["名字","年齡","住址"]' \
--form 'jsonData2={"name":"zhaoliu","age":20}' \
'http://192.168.3.24:9002/curl/file/upload'


2.2.4--data和--data-urlencode和--url-query
--data 和 --data-urlencode
-d / --data會(huì)對(duì)傳入的數(shù)據(jù)進(jìn)行 URL 編碼處理,默認(rèn)會(huì)自動(dòng)使用application/x-www-form-urlencoded表單的Post請(qǐng)求- 而我們需要發(fā)送Get請(qǐng)求,并且需要通過(guò)url傳遞參數(shù),因此需要明確的通過(guò)
--get配置項(xiàng)指定Get的請(qǐng)求方式 - 當(dāng)url中有漢字等字符時(shí),使用
--data-urlencode配置項(xiàng)進(jìn)行編碼
curl --get -v -w '\n' \ -d 'INFILE=/data/path/out' \ --data 'JobId=CCMPLTEST01' \ --data 'OTFILE=/data/path/in' \ --data-urlencode 'name=賈 飛天' \ 'http://192.168.3.24:9002/curl/get'

--url-query
- 2022年的
7.87.0版本的curl追加了--url-query配置項(xiàng),專門用來(lái)處理url傳參 - 使用
--url-query配置項(xiàng)之后,使用Get請(qǐng)求時(shí),就不用專門聲明使用Get請(qǐng)求了 - Post,Put,Delete方式也支持使用
--url-query
curl -v -w '\n' \ --url-query 'JobId=CCMPLTEST01' \ --url-query 'OTFILE=/data/path/in' \ --url-query 'INFILE=/data/path/out' \ --url-query 'name=賈 飛天' \ 'http://192.168.3.24:9002/curl/get'

2.2.5-T⇔--upload-file
- 指定文件上傳
--request PUT可省略--header 'Content-Type: text/plain':顯式指定上傳文件的 MIME 類型,即告訴服務(wù)器這個(gè)文件屬于哪種類型
curl --request PUT \ -v -w '\n' \ --header 'X-Filename: grep_cmd.txt' \ --header 'Content-Type: text/plain' \ --upload-file 'grep_cmd.txt' \ 'http://192.168.3.24:9002/curl/put/file'

2.3 head相關(guān)
2.3.1-A⇔--user-agent指定用于代理字符串
curl命令,默認(rèn)情況下使用User-Agent: curl/8.15.0,但是部分網(wǎng)站可能不允許User-Agent: curl/8.15.0的請(qǐng)求訪問(wèn)

可以通過(guò)下面3種方式指定User-Agent,模擬瀏覽器訪問(wèn)
# --user-agent 的方式 curl --get \ -v -w '\n' \ --user-agent 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36' \ 'http://192.168.3.24:9002/curl/get' # -A 的方式 curl -G \ -v -w '\n' \ -A 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36' \ 'http://192.168.3.24:9002/curl/get' # --header 的方式 curl -X GET \ -v -w '\n' \ --header 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36' \ 'http://192.168.3.24:9002/curl/get'
2.3.2-H⇔--header指定請(qǐng)求頭
用于在發(fā)起請(qǐng)求的時(shí)候,添加請(qǐng)求頭
curl -v -w '\n' \ --header 'X-USER-ID: jmw110' \ -H 'X-TOKEN: abc123' \ --data 'order_id=order134' \ --data 'event=PLACED' \ 'http://192.168.3.24:9002/curl/form/post'

2.3.3-i和-I和-D和--show-headers
-i:在輸出中包含服務(wù)器響應(yīng)的 HTTP 頭部信息
-i⇔--include- curl命令,默認(rèn)只顯示響應(yīng)體,加上
-i就能看到響應(yīng)頭信息。 - 如果想調(diào)試 HTTP 接口、看響應(yīng)狀態(tài)碼、內(nèi)容類型、Cookie 等信息,
-i很有用。

-I:只請(qǐng)求并顯示響應(yīng)頭,不下載正文,相當(dāng)于發(fā)起HEAD請(qǐng)求
-I⇔--head- 常用于檢查文件是否存在、獲取資源信息(如大小、類型、是否支持?jǐn)帱c(diǎn)續(xù)傳等)
- 如果要測(cè)試資源是否存在、獲取內(nèi)容大小,用
-I就夠了 - 如果要調(diào)試接口輸出結(jié)構(gòu),用
-i更合適。

--show-headers的作用和-i相同,語(yǔ)義化更好,在最新的curl版本支持

-D:將服務(wù)器返回的 HTTP 響應(yīng)頭(headers)輸出到指定文件或標(biāo)準(zhǔn)輸出
-D⇔--dump-header-o /dev/null:服務(wù)器的響應(yīng)體(返回的數(shù)據(jù))不顯示到控制臺(tái)
curl -sS -D - \ 'https://amazon.com' curl -sS -D - \ -o /dev/null \ 'https://amazon.com'

2.4 輸出相關(guān)
2.4.1-s和-S
-s:Silent 模式,不顯示進(jìn)度條等無(wú)關(guān)輸出-S:配合-s使用,報(bào)錯(cuò)時(shí),顯示錯(cuò)誤信息
# 顯示進(jìn)度條 curl --get -w '\n' \ 'http://192.168.3.24:9002/curl/get' | jq . # 不顯示進(jìn)度條, 但連接不上服務(wù)器報(bào)錯(cuò)時(shí), 什么也不輸出 curl --get -w '\n' \ -s \ 'http://你好.168.3.24:9022/curl/get' | jq . # 不顯示進(jìn)度條, 但連接不上服務(wù)器報(bào)錯(cuò)時(shí), 顯示報(bào)錯(cuò)信息 curl --get -w '\n' \ -sS \ 'http://你好.168.3.24:9022/curl/get' | jq .
- 當(dāng)我們使用
jq命令處理json數(shù)據(jù)的時(shí)候,顯示多余的進(jìn)度條 - 使用
-s之后,多余的進(jìn)度條消失

- 但是使用
-s之后,當(dāng)我們請(qǐng)求不存在的ip地址時(shí),并不會(huì)顯示任何錯(cuò)誤 - 使用
-sS之后,錯(cuò)誤信息被顯示在控制臺(tái)上

2.4.2-f⇔--fail快速失敗
- 表示在請(qǐng)求失敗時(shí)(即服務(wù)器返回 400 或更高的狀態(tài)碼)不要輸出 HTML 錯(cuò)誤頁(yè)面內(nèi)容,只返回非零退出碼和錯(cuò)誤信息。
- 在寫腳本時(shí)非常有用,可以避免把錯(cuò)誤頁(yè)面當(dāng)作有效內(nèi)容處理。
# 會(huì)顯示出錯(cuò)誤時(shí)的json信息 curl --get 'https://api.github.com/users/user_not_exist' # 服務(wù)器返回404狀態(tài)碼時(shí),直接報(bào)錯(cuò) curl --get --fail 'https://api.github.com/users/user_not_exist'
- 具體效果如下

2.4.3-v
- 詳細(xì)模式(verbose),會(huì)顯示整個(gè) HTTP 請(qǐng)求/響應(yīng)過(guò)程,包括連接過(guò)程、請(qǐng)求頭、發(fā)送的數(shù)據(jù)等

2.4.4-w⇔--write-out
- 在請(qǐng)求完成后輸出特定的信息(例如狀態(tài)碼、總時(shí)間、IP、響應(yīng)大小等),用來(lái)輸出追加的情報(bào)
| 占位符 | 含義 |
|---|---|
| %{http_code} | HTTP 響應(yīng)狀態(tài)碼(如 200、404) |
| %{time_total} | 總耗時(shí)(秒),包括解析 DNS、建立連接等 |
| %{time_namelookup} | DNS 查詢耗時(shí)(秒) |
| %{time_connect} | 建立連接所耗時(shí)間(秒) |
| %{time_starttransfer} | 服務(wù)器開始響應(yīng)的時(shí)間(秒) |
| %{remote_ip} | 連接的遠(yuǎn)程服務(wù)器 IP |
| %{remote_port} | 遠(yuǎn)程服務(wù)器端口 |
| %{size_download} | 下載的字節(jié)數(shù) |
| %{size_upload} | 上傳的字節(jié)數(shù) |
| %{url_effective} | 最終的 URL(跟隨跳轉(zhuǎn)后的地址) |
| %{content_type} | 響應(yīng)的 Content-Type 值 |
-w '\n':實(shí)現(xiàn)換行

顯示 IP、端口、響應(yīng)時(shí)間
- 注意:因?yàn)?code>-w中有
\n換行符,所以需要使用單引號(hào)而不是雙引號(hào)
curl -s -o /dev/null \
-w '\nIP: %{remote_ip}\n端口: %{remote_port}\n響應(yīng)耗時(shí): %{time_starttransfer} 秒\n' \
'https://example.com'

其他用法
apluser@FengYeHong-HP:~$ curl -sSf -o /dev/null \
-w '\nStatus: %{http_code}\nTime: %{time_total}s\n' \
'https://example.com'
Status: 200
Time: 0.538273s
# http頭以外的情報(bào)輸出為json
curl -sS -w '%{json}' -o /dev/null http://example.com | jq | head
# 將http頭的情報(bào)輸出為json的形式
curl -sS -w '%{header_json}' -o /dev/null 'https://api.github.com/users/fengyehong123' | jq | head
# 還可以這么用 %header{key的名字}
curl -sS -w '%header{content-length}\n' -o /dev/null 'https://api.github.com/users/fengyehong123'
2.4.5-o⇔--output和-O將響應(yīng)內(nèi)容保存到指定文件中
-o后面的文件名是你手動(dòng)指定的,即使 URL 里寫著別的名字,它也會(huì)被保存為你指定的那個(gè)文件名。- 如果文件已存在,curl 會(huì)覆蓋它,而不會(huì)提示。

-O(大寫)使用url里面的文件名進(jìn)行下載

2.4.6-L⇔--location重定向
- curl請(qǐng)求默認(rèn)是不會(huì)重定向的
- 使用
-L配置項(xiàng)可以讓請(qǐng)求跟隨服務(wù)器進(jìn)行重定向

2.5 其他參數(shù)
2.5.1-m和--connect-timeout
-m:最大請(qǐng)求時(shí)間為 ?秒,超時(shí)中斷--connect-timeout:最大連接時(shí)間為 ?秒,連接不上就失敗

2.5.2--compressed
- 告訴服務(wù)器你可以接受壓縮(比如 gzip、deflate、br)后的響應(yīng)內(nèi)容,同時(shí)在接收后自動(dòng)為你解壓。
- curl 默認(rèn)不發(fā)送
Accept-Encoding,所以你需要手動(dòng)加--compressed來(lái)啟用。

2.5.3--variable和--expand-url
2023年11月份release的8.3.0版本的curl命令添加了變量和變量展開功能
--variable:用于定義變量--expand-*:用于展開變量--expand-url:用來(lái)拓展url
curl -sS -w '\n' -v \
--variable 'port=9002' \
--expand-url 'http://192.168.3.24:{{port}}/curl/get/'

2.5.4--referer
簡(jiǎn)介
- 在 HTTP 請(qǐng)求中,
Referer表示請(qǐng)求來(lái)源頁(yè)面的 URL。 - 服務(wù)器經(jīng)常用它來(lái)判斷請(qǐng)求是否是從合法頁(yè)面跳轉(zhuǎn)過(guò)來(lái)的。
- 用
--referer就能手動(dòng)偽造或修改這個(gè)字段。
常見用途
- 繞過(guò)防盜鏈,比如有些網(wǎng)站的圖片或文件只允許從自己頁(yè)面訪問(wèn),檢查
Referer是否是自己域名。 - 模擬瀏覽器行為,模擬從某個(gè)網(wǎng)頁(yè)點(diǎn)擊進(jìn)入另一個(gè)頁(yè)面。
- 測(cè)試網(wǎng)站安全性。開發(fā)或測(cè)試時(shí),可以偽造來(lái)源,檢查服務(wù)器是否依賴 Referer 來(lái)做安全控制。
curl --referer 'http://www.example.com' \ -fsS -D - -o /dev/null \ --location 'http://www.example.com'

三. 提交數(shù)據(jù)的常見格式
3.1 url傳參
curl --get -v -w '\n' \ -d 'INFILE=/data/path/out' \ --data 'JobId=CCMPLTEST01' \ --data 'OTFILE=/data/path/in' \ --data-urlencode 'name=賈 飛天' \ 'http://192.168.3.24:9002/curl/get'
3.2application/x-www-form-urlencoded表單提交
curl -v -w '\n' \ --data 'order_id=order134' \ --data 'event=PLACED' \ 'http://192.168.3.24:9002/curl/form/post'
3.3multipart/form-data表單提交
- 在普通的表單提交中(
application/x-www-form-urlencoded),所有數(shù)據(jù)都會(huì)被編碼為 URL 參數(shù)形式。 - 但是普通的表單提交不能處理文件,也不能很好支持二進(jìn)制數(shù)據(jù)。
- 所以 HTTP 引入了
multipart/form-data來(lái)實(shí)現(xiàn)一部分是文本,一部分是文件/二進(jìn)制的提交功能。
curl -v -w '\n' \
--form 'file1=@grep_cmd.txt' \
--form 'jsonData1={"name":"wangwu","age":30,"card":["abc","bbc"]}' \
--form 'jsonData1=測(cè)試信息' \
--form 'jsonData1=["名字","年齡","住址"]' \
--form 'jsonData2={"name":"zhaoliu","age":20}' \
'http://192.168.3.24:9002/curl/file/upload'
3.4 json數(shù)據(jù)
curl -v -w '\n' \
--header 'Content-Type: application/json' \
--data-binary '{"order_id": "order134"}' \
'http://192.168.3.24:9002/curl/json/post'
curl -v -w '\n' \
--json '{"order_id": "order134"}' \
'http://192.168.3.24:9002/curl/json/post'
3.5 xml數(shù)據(jù)
curl -v -X POST \
--location https://postman-echo.com/post \
-H "Content-Type: application/xml; charset=UTF-8" \
-d '<?xml version="1.0" encoding="UTF-8"?>
<order>
<orderId>123</orderId>
<total>99.9</total>
</order>'
四. 案例
直接執(zhí)行網(wǎng)絡(luò)上的腳本
- 下面這2種寫法相同
- 執(zhí)行此類腳本一定要謹(jǐn)慎,要確保網(wǎng)絡(luò)腳本是可信任的
- curl命令下載到的腳本內(nèi)容直接作為
標(biāo)準(zhǔn)輸入傳給bash執(zhí)行,也就是說(shuō),這個(gè)腳本文件根本不會(huì)保存到本地,而是邊下載邊執(zhí)行。
curl -fsSL 'https://raw.githubusercontent.com/fengyehong123/BashStudy/refs/heads/main/99-技巧/04-隨機(jī)/01-$RANDOM隨機(jī)數(shù)和隨機(jī)字符串.sh' | bash bash <(curl -fsSL 'https://raw.githubusercontent.com/fengyehong123/BashStudy/refs/heads/main/99-技巧/04-隨機(jī)/01-$RANDOM隨機(jī)數(shù)和隨機(jī)字符串.sh')
?下載網(wǎng)站上的圖片
tr ' ' '\n':將空格替換為換行符,確保每一個(gè)網(wǎng)址一行
xargs -n 1 -P 10:
-n 1→ 每次取 1 個(gè)參數(shù) 傳給 command-P 10→ 最多同時(shí)運(yùn)行 10 個(gè) command 進(jìn)程(并行執(zhí)行)
--retry 3 --retry-delay 2:
--retry 5→ 失敗時(shí)最多重試 3 次--retry-delay 2→ 兩次重試間隔 2 秒
echo https://www.itzmx.com/{1..499}.webp | \
tr ' ' '\n' | \
xargs -n 1 -P 10 \
curl -s -O --retry 3 --retry-delay 2
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Linux 中不輸入密碼運(yùn)行 sudo 命令的方法
sudo命令允許受信任的用戶作為另一個(gè)用戶運(yùn)行程序,默認(rèn)情況下是root用戶。這篇文章主要介紹了Linux 中不輸入密碼運(yùn)行 sudo 命令的方法,需要的朋友可以參考下2019-07-07
linux下安裝配置svn獨(dú)立服務(wù)器的步驟分享
subversion(以下簡(jiǎn)稱svn)是近年來(lái)崛起的版本管理工具,是cvs的接班人。 svn服務(wù)器有2種運(yùn)行方式:獨(dú)立服務(wù)器和借助apache。2種方式各有利弊2011-05-05
Linux系統(tǒng)中查看文件夾或文件大小的命令小結(jié)
在日常運(yùn)維和開發(fā)工作中,了解文件夾和文件占用的磁盤空間是非常重要的,在 Linux 系統(tǒng)中,我們可以使用一些簡(jiǎn)單的命令來(lái)查看文件夾或文件的大小,下面小編就來(lái)詳細(xì)介紹一下吧2025-08-08
linux下用cron定時(shí)執(zhí)行任務(wù)的方法
呵呵,比較有用,網(wǎng)上摘抄的。留著備用吧。2008-06-06
linux系統(tǒng)虛擬主機(jī)開啟支持Swoole Loader擴(kuò)展的方法
這篇文章主要介紹了linux系統(tǒng)虛擬主機(jī)開啟支持Swoole Loader擴(kuò)展的方法,需要的朋友可以參考下2020-12-12
VSCode連接實(shí)驗(yàn)室服務(wù)器Xshell,Xftp向服務(wù)器傳輸文件的方法(win10)
這篇文章主要介紹了VSCode連接實(shí)驗(yàn)室服務(wù)器,Xshell,Xftp向服務(wù)器傳輸文件(win10),本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-08-08
CentOS解壓rar格式包實(shí)現(xiàn)過(guò)程
文章主要介紹了如何解決yum安裝報(bào)錯(cuò)的問(wèn)題,提供了離線安裝32位和64位包的方法,并詳細(xì)介紹了如何使用unrar命令進(jìn)行解壓2026-01-01

