curl(Client URL)是一个开源的命令行工具,用于在终端中传输数据,支持多种协议(HTTP/HTTPS/FTP/SCP等)。
curl 核心功能:
功能 | 示例 |
---|---|
下载文件 | curl -O https://example.com/file.zip (保存为原文件名)curl -o custom.zip https://example.com/file.zip (自定义文件名) |
上传文件 | curl -T file.txt ftp://example.com/upload/ --user username:password |
发送HTTP请求 | curl https://api.example.com/data (GET请求)curl -X POST -d "key=value" https://api.example.com/post (POST请求) |
调试API | curl -v https://api.example.com (显示详细请求/响应头)curl -I https://example.com (仅获取响应头) |
断点续传 | curl -C - -O https://example.com/largefile.zip |
代理访问 | curl -x http://proxy:port https://example.com |
curl 参数详解:
参数 | 作用 |
---|---|
-X |
指定HTTP方法(GET/POST/PUT/DELETE等) |
-d |
发送POST请求的数据(如 -d "name=John&age=30" ) |
-H |
添加请求头(如 -H "Content-Type: application/json" ) |
-o |
将输出保存到指定文件(覆盖模式) |
-O |
将输出保存为远程文件的原始名称 |
-L |
跟随重定向(自动跳转到新URL) |
-C - |
断点续传(自动检测本地已下载部分继续) |
-u |
用户名和密码认证(如 -u username:password ) |
-k |
忽略SSL证书验证(不安全,仅测试用) |
-v |
显示详细请求/响应信息(调试用) |
--limit-rate |
限制下载速度(如 --limit-rate 1m 限制为1MB/s) |
curl 实战案例:
1、下载文件并显示进度条
curl -# -O https://example.com/file.iso # -# 显示进度条
2、发送JSON数据的POST请求
curl -X POST -H "Content-Type: application/json" -d '{"user":"admin","pass":"123"}' https://api.example.com/login
3、测试网站可访问性
curl -I https://example.com # 仅获取HTTP响应头
curl -s -o /dev/null -w "%{http_code}" https://example.com # 仅返回状态码(如200)
4、通过FTP上传文件
curl -T localfile.txt ftp://ftp.example.com/upload/ --user ftpuser:ftppass
5、调试HTTPS证书问题
curl -v https://example.com # 查看证书详情
curl -k https://self-signed.example.com # 忽略证书错误(慎用)