curl7 命令行使用
Curl 7 命令行使用指南
[!NOTE] Windows PowerShell 用户注意
- 在 Windows PowerShell 中,
curl通常是Invoke-WebRequest的别名。要确保使用原生的 curl,请显式使用curl.exe。- PowerShell 对引号的处理与 Bash 不同。发送 JSON 数据时,不要单引号包围双引号 (例如
-d '{"a":1}'在 PS 中会失败)。
- 推荐写法 1 (转义双引号):
-d "{\"name\": \"value\"}"- 推荐写法 2 (使用 @file):
-d @data.json
HTTP 协议
基础请求与网络
# 访问 IPv6 地址
curl "http://[fe80::79c8:7bd1:cf97:aef3%8]:80"
# 使用 Socks5 代理
curl --socks5-hostname 127.0.0.1:19996 http://www.google.com/
curl -x socks5://127.0.0.1:19996 http://www.google.com/
# 失败重试 (--retry 指定重试次数)
curl --url http://www.google.com/ --retry 3
# 指定请求方法 (-X)
curl -X POST http://localhost:7777/ping
# 自定义 DNS 解析 (--resolve host:port:address)
# 相当于修改 /etc/hosts,但在命令执行期间生效
curl --resolve example.com:7777:127.0.0.1 http://example.com:7777/ping
# 跟随重定向 (-L)
# 自动跟随 3XX 响应码跳转
curl -L http://127.0.0.1:7777/ping
调试与输出控制
# 详细模式 (-v)
curl -v http://example.com
# 更详细的 Trace 模式 (包含 Hex dump)
curl --trace dump.txt http://example.com
# 自定义输出格式 (-w)
# 可用变量参考: https://everything.curl.dev/usingcurl/verbose/writeout
curl -w "\nType: %{content_type}\nCode: %{response_code}\n" http://127.0.0.1:7777/ping
# 静默模式 (不输出进度条,常配合 -w 使用)
curl -s -w "%{http_code}" -o /dev/null http://example.com
文件下载
# 保存为指定文件名 (-o)
curl --url http://127.0.0.1:7777/ping -o output.txt
# 避免覆盖已存在文件 (--no-clobber)
curl --url http://127.0.0.1:7777/ping --no-clobber -o output.txt
# 使用远程文件名保存 (-O)
# 注意:这会使用 URL 的最后一部分作为文件名
curl -O http://127.0.0.1:7777/ping
请求头与元数据
# 添加请求头 (-H)
curl -H "X-First-Name: Joe" -H "User-Agent: MyBot/1.0" http://127.0.0.1:7777/ping
# 仅获取响应头 HEAD 请求 (-I)
curl -I http://127.0.0.1:7777/ping
数据发送 (POST/PUT)
# 发送 JSON 数据 (--data-raw)
# Linux/Bash:
curl --data-raw '{"name": "ajanuw"}' -H 'Content-type: application/json' http://127.0.0.1:7777/ping
# Windows PowerShell (注意转义):
curl.exe --data-raw "{\"name\": \"ajanuw\"}" -H "Content-type: application/json" http://127.0.0.1:7777/ping
# 从文件发送数据 (-d @filename)
curl -d @file.json -H 'Content-type: application/json' http://127.0.0.1:7777/ping
# 发送表单数据 application/x-www-form-urlencoded (-d)
curl -d 'name=ajanuw&age=12' http://127.0.0.1:7777/ping
# GET 请求携带数据 (-G 配合 -d)
# 这会自动把数据拼接到 URL 查询参数中
curl -G -d 'name=ajanuw&age=12' http://127.0.0.1:7777/ping
文件上传 (Multipart)
# 表单文件上传 (-F)
# 对应 HTML form 的 multipart/form-data
curl -F profile=@portrait.jpg https://example.com/upload.cgi
# 多字段上传
curl -F name=John -F shoesize=11 https://example.com/
# 从文件读取内容作为字段值 (注意 < 符号)
curl -F "story=<hugefile.txt" https://example.com/
# 显式指定上传文件名
curl -F "file=@localfile;filename=custom_name.txt" example.com
SFTP 协议
# 列出目录
curl -k "sftp://192.168.149.128/root/" --user "root:123"
# 使用不同语法指定用户
curl sftp://root:123@192.168.149.128/root/
# 上传文件 (-T)
curl -k "sftp://192.168.149.128/root/" --user "root:123" -T "./local_file.txt"
# 上传并自动创建不存在的目录 (--ftp-create-dirs)
curl -k "sftp://192.168.149.128/root/newdir/" --user "root:123" -T "./local_file.txt" --ftp-create-dirs
# 下载文件
curl -k "sftp://192.168.149.128/root/remote_file.txt" --user "root:123" -o "./local_download.txt"
# 下载并自动创建本地目录 (--create-dirs)
curl -k "sftp://192.168.149.128/root/remote_file.txt" --user "root:123" -o "./d/u.txt" --create-dirs
SFTP/FTP 命令执行 (-Q)
使用 -Q 或 --quote 可以在传输前后执行命令。
- 前缀
*: 即使失败也继续 (FTP) - 前缀
-: 传输完成后执行
常见 SFTP 命令:
| 命令 | 描述 |
|---|---|
mkdir directory |
创建目录 |
rm file |
删除文件 |
rmdir directory |
删除空目录 |
rename source target |
重命名 |
chmod mode file |
修改权限 |
chown user file |
修改所有者 |
示例:
# 创建目录
curl -k "sftp://192.168.149.128/root/" --user "root:123" -Q '-mkdir sftp_newdir'
# 重命名文件
curl -k "sftp://192.168.149.128/root/" --user "root:123" -Q '-rename old.txt new.txt'
# 删除目录
curl -k "sftp://192.168.149.128/root/" --user "root:123" -Q '-rmdir sftp_newdir'
FILE 协议
用于读取本地文件,常用于测试。
# Linux/Mac
curl file:///etc/hosts
# Windows
curl file:///C:/Windows/System32/drivers/etc/hosts
使用配置文件 (-K)
当命令行参数过长时,可以将参数写入文件。
Format (curl_config.txt):
-X POST
--url http://127.0.0.1:7777/ping
-d "msg=hello"
Usage:
curl -K curl_config.txt
电子邮件协议 (POP3/IMAP/SMTP)
Curl 支持多种邮件协议的读写操作。通常需要 --ssl 或 --ssl-reqd 来保证安全。
- POP3 (110) / POP3S (995): 接收邮件
- IMAP (143) / IMAPS (993): 接收/管理邮件
- SMTP (25) / SMTPS (465): 发送邮件
# pop3: 列出邮件
curl pop3://mail.example.com -u 'user:password'
# pop3: 下载第 3 封邮件
curl pop3://mail.example.com/3 -u 'user:password'
# pop3: 删除第 1 封邮件 (使用 --request DELE)
curl pop3://smtp.qq.com/1 -u 'user:pass' --ssl --request DELE
# imap: 从 Drafts 文件夹获取特定邮件
curl "imap://smtp.qq.com/Drafts;MAILINDEX=1" -u 'user:pass' --ssl

浙公网安备 33010602011771号