😈 curl命令使用教程

curl 是一个强大的命令行工具,用于在终端中传输数据,支持多种协议(如 HTTP、HTTPS、FTP、SMTP 等),适用于测试 Web 接口、下载文件、调试服务等场景。它是开发人员调试 API 和下载文件时常用的工具,熟练掌握其常用参数可以显著提升开发效率。


1、安装 curl

大多数 Linux 发行版和 macOS 都预装了 curl。如果没有安装,可以通过以下方式安装:

1.1、Windows

Windows 10Windows 11 开始,系统已经内置了 curl 命令,官网下载:https://curl.se/download.html
下载安装包,手动安装好后,将 curl.exe 添加到系统环境变量 PATH。

1
2
# 查看版本
curl --version

1.2、Ubuntu / Debian

1
sudo apt update && sudo apt install curl

1.3、CentOS / RHEL

1
sudo yum install curl

1.4、macOS (使用 Homebrew)

1
brew install curl

2、基本用法

2.1、获取网页内容(默认GET 请求)

1
2
3
# 语法糖
curl [选项] [URL]
curl http://example.com

这会输出请求网页的 HTML 源码到终端。

2.2、将结果保存到文件

  • 使用 -o 保存文件并指定名称:
1
curl -o index.html http://example.com
  • 使用 -O 保存为远程文件名相同的名字:
1
curl -O https://example.com/file.txt 

2.3、发送 POST 请求

1
curl -X POST -d "param1=value1&param2=value2" http://example.com/api
  • -X POST:指定请求方法为 POST。
  • -d:发送的数据体。

2.4、设置请求头

1
2
3
curl -H "Content-Type: application/json" \
​ -H "Authorization: Bearer YOUR_TOKEN" \
​ http://api.example.com/data
  • -H:添加请求头。

2.5、使用 JSON 数据发送 POST 请求

1
2
3
4
curl -X POST \
​ -H "Content-Type: application/json" \
​ -d '{"username":"user1", "password":"pass1"}' \
​ http://api.example.com/login

2.6、使用 Basic Auth 登录

1
curl -u username:password http://example.com/secure

2.7、跟随重定向

默认情况下,curl 不会自动跟随重定向。添加 -L 参数来启用:

1
curl -L http://example.com

2.8、显示详细信息

使用 -v 可以查看详细的请求和响应过程(包括 headers):

1
curl -v http://example.com

3、参数列表

参数 作用 示例
-X 指定 HTTP 方法 curl -X DELETE https://api.example.com/item/123
-H 添加请求头
-d 发送请求体数据 curl -d “param1=value1” https://example.com
-o 输出到文件 curl -o download.zip https://example.com/file.zip
-L 跟随重定向 curl -L https://short.url
-k 跳过 SSL 验证(不安全) curl -k https://self-signed.badssl.com
-u 基本认证 curl -u username:password https://api.example.com

4、示例合集

功能 命令
获取网页内容 curl https://example.com
下载文件 curl -O https://example.com/file.zip
发送 JSON 到 API curl -X POST -H "Content-Type: application/json" -d '{"key":"value"}' http://api.example.com
登录认证请求 curl -u username:password https://api.example.com
获取 HTTP 状态码 curl -I http://example.com
跟随重定向 curl -L http://example.com

5、📚 参考资料


😈 curl命令使用教程
http://pygo2.top/articles/1964/
作者
mingliang.gao
发布于
2024年1月9日
许可协议