Curl 用法

✅ 1️⃣ curl 是什么?

curl 是一个命令行下的万能网络请求工具,可以用来:

  • 访问网页(GET)
  • 提交表单(POST)
  • 上传文件(PUT/POST)
  • 下载文件
  • 调试 HTTP/HTTPS 请求
  • 和 REST API、GraphQL、SOAP 等对接
  • 做性能测试、验证 header、cookie、代理、证书等

几乎所有后端、前端、运维、DevOps、渗透测试、API 调试都会用它。

✅ 2️⃣ 最基本用法

GET 请求

curl https://example.com

这会把网页内容输出到终端。

把响应保存到文件

curl -o filename.html https://example.com
# 或者
curl -O https://example.com/file.zip  # -O 会使用远端文件名

查看完整请求头 + 响应头

curl -v https://example.com

✅ 3️⃣ POST 请求

提交表单

curl -X POST -d "username=allen&password=123" https://example.com/login

提交 JSON

curl -X POST -H "Content-Type: application/json" -d '{"name":"allen","age":30}' https://example.com/api

✅ 4️⃣ 自定义请求头

curl -H "Authorization: Bearer <token>" https://example.com/protected

可以加多个:

curl -H "X-API-KEY: abc123" -H "Accept: application/json" ...

单个 Cookie:

curl -b "sessionid=12345" https://example.com/profile

使用 Cookie 文件:

curl -b cookies.txt https://example.com

✅ 6️⃣ 上传文件

表单上传(模拟 multipart/form-data):

curl -F "file=@/path/to/file.txt" https://example.com/upload

✅ 7️⃣ 使用代理

HTTP 代理:

curl -x http://127.0.0.1:8080 https://example.com

Socks5 代理(如 Shadow(敏感词)socks、V2(敏感词)Ray):

curl --socks5 127.0.0.1:1080 https://example.com

✅ 8️⃣ 认证

基本认证(Basic Auth):

curl -u username:password https://example.com

✅ 9️⃣ 跳过 HTTPS 证书校验(不推荐生产用)

curl -k https://self-signed.badssl.com/

✅ 1️⃣0️⃣ 限制带宽 / 超时

限速:

curl --limit-rate 100k https://example.com/bigfile.zip

设置连接超时:

curl --connect-timeout 5 https://example.com

✅ 1️⃣1️⃣ 保存并继续下载(断点续传)

curl -C - -O https://example.com/bigfile.zip

✅ 1️⃣2️⃣ 常见组合例子

调用 REST API 并格式化输出(配合 jq)

curl -s https://api.github.com/users/allen | jq .

测网站是否支持 HTTP2

curl -I --http2 https://example.com

下载大文件时显示进度条

curl -# -O https://example.com/bigfile.iso

✅ 1️⃣3️⃣ 查看 curl 帮助

curl --help
curl --manual

✅ 1️⃣4️⃣ curl 配合 openssl 做 HTTPS 验证

✅ 一句话总结

curl = 调试网络接口 / 批量抓数据 / 做自动化脚本 / 验证 HTTPS、代理、API 调用,全都能用。
是任何做网络、后端、API、DevOps、黑客、渗透都绕不开的瑞士军刀。


posted @ 2025-07-04 19:17  江南烟雨梦  阅读(52)  评论(0)    收藏  举报