Curl 命令详解与实战技巧

掌握 HTTP 请求调试利器,从基础参数到高级用法

一、Curl 是什么

第一次接触 Curl 是在三年前,那时候刚入门爬虫,看到前辈们在命令行里敲几行代码就能测试接口,觉得特别酷。后来慢慢发现,Curl 不仅是测试工具,更是理解 HTTP 协议的最佳途径。

Curl 是一个命令行工具,用来发送 HTTP 请求。它支持几乎所有常见的协议:HTTP、HTTPS、FTP、SFTP,甚至邮件协议。在爬虫开发、API 调试、自动化测试这些场景里,Curl 都是必备工具。

为什么学 Curl
  • 浏览器开发者工具能直接复制 Curl 命令,快速复现请求
  • 不需要写代码就能测试接口,验证参数是否正确
  • 理解 Curl 的参数,等于理解了 HTTP 请求的各个组成部分
  • 很多服务器的接口文档直接提供 Curl 示例

二、安装与验证

2.1 检查是否已安装

大部分 Linux 和 macOS 系统都预装了 Curl。Windows 10 之后的版本也自带了。

curl --version

如果看到版本信息,说明已经装好了。如果提示找不到命令,需要手动安装。

2.2 安装方法

# macOS brew install curl # Ubuntu/Debian sudo apt-get install curl # Windows # 推荐直接下载官方二进制文件,或者使用 Git Bash

三、基础用法

3.1 最简单的 GET 请求

# 获取网页内容 curl https://api.github.com # 保存到文件 curl https://api.github.com -o github.json # 跟随重定向 curl -L https://bit.ly/3xKk5

3.2 常用参数速查

参数 全称 作用
-X --request 指定请求方法(GET、POST、PUT 等)
-H --header 添加请求头
-d --data 发送表单数据
-o --output 输出到文件
-L --location 跟随重定向
-v --verbose 显示详细过程
-s --silent 静默模式,不显示进度

四、请求头设置

请求头是 HTTP 请求的重要组成部分。很多反爬机制都会检查请求头的合法性。

4.1 设置 User-Agent

curl -H "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36" \ https://example.com

4.2 设置多个请求头

curl -H "Accept: application/json" \ -H "Authorization: Bearer token123" \ -H "Content-Type: application/json" \ https://api.example.com/data
小技巧:-H 参数可以添加任意请求头。如果某个请求头已经存在,新的值会覆盖旧的。

五、发送数据

5.1 发送表单数据

# 方式1:-d 参数(默认 Content-Type: application/x-www-form-urlencoded) curl -X POST \ -d "username=admin&password=123456" \ https://example.com/login # 方式2:多个 -d 参数会自动拼接 curl -X POST \ -d "username=admin" \ -d "password=123456" \ https://example.com/login

5.2 发送 JSON 数据

curl -X POST \ -H "Content-Type: application/json" \ -d '{"username":"admin","password":"123456"}' \ https://api.example.com/login

5.3 上传文件

# 单文件上传 curl -X POST \ -F "file=@/path/to/file.jpg" \ https://example.com/upload # 多文件上传 curl -X POST \ -F "file1=@/path/to/file1.jpg" \ -F "file2=@/path/to/file2.jpg" \ https://example.com/upload # 带额外参数的文件上传 curl -X POST \ -F "file=@/path/to/file.jpg" \ -F "description=我的图片" \ https://example.com/upload

注意:-F 参数会自动设置 Content-Type: multipart/form-data,不要手动设置这个头,否则会冲突。

六、认证方式

6.1 Basic 认证

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

6.2 Bearer Token

curl -H "Authorization: Bearer your_token_here" \ https://api.example.com/protected

6.3 API Key

# 通过查询参数传递 curl "https://api.example.com/data?api_key=your_key" # 通过请求头传递 curl -H "X-API-Key: your_key" \ https://api.example.com/data

7.1 发送 Cookie

curl -H "Cookie: session_id=abc123; user_id=456" \ https://example.com/profile

7.2 保存和读取 Cookie

# 登录并保存 Cookie 到文件 curl -X POST \ -d "username=admin&password=123456" \ -c cookies.txt \ https://example.com/login # 使用保存的 Cookie 访问其他页面 curl -b cookies.txt \ https://example.com/profile
爬虫场景:先用浏览器登录,复制 Cookie,然后直接用 -b 参数带上,就能访问需要登录的页面了。

八、代理设置

# HTTP 代理 curl -x http://127.0.0.1:8080 https://api.ipify.org?format=json # 带认证的代理 curl -x http://username:password@proxy.example.com:8080 \ https://api.example.com # SOCKS5 代理 curl -x socks5://127.0.0.1:1080 https://api.example.com

九、高级技巧

9.1 只获取响应头

curl -I https://example.com

9.2 跟随重定向并显示整个过程

curl -L -v https://bit.ly/3xKk5

9.3 限制下载速度

# 限制为 100KB/s curl --limit-rate 100K -O https://example.com/large_file.zip

9.4 断点续传

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

9.5 发送 PUT/DELETE 请求

# PUT 请求 curl -X PUT \ -H "Content-Type: application/json" \ -d '{"name":"new_name"}' \ https://api.example.com/resource/123 # DELETE 请求 curl -X DELETE https://api.example.com/resource/123

十、调试技巧

10.1 查看完整的请求和响应

curl -v https://example.com

输出中会显示:

  • > 开头的行:发送的请求内容
  • < 开头的行:接收的响应头
  • * 开头的行:连接信息

10.2 将请求信息输出到文件

# 保存响应头 curl -D headers.txt https://example.com # 保存响应内容 curl -o body.html https://example.com # 同时保存 curl -D headers.txt -o body.html https://example.com

10.3 格式化 JSON 输出

# 配合 Python 格式化 JSON curl -s https://api.github.com | python -m json.tool # 或者使用 jq 工具(需要安装) curl -s https://api.github.com | jq '.'
调试流程:先用 -v 看完整请求,确认参数正确;然后用 -s 静默模式获取纯数据;最后用管道格式化输出。

十一、总结

Curl 的命令参数看起来很多,但常用的就那几个。记住这个组合,能应付 90% 的场景:

curl -X POST -H "Header: value" -d "data" -o output.txt URL

做爬虫开发时,我的典型工作流程是:

  1. 在浏览器开发者工具里找到目标请求
  2. 右键复制为 Curl 命令
  3. 在命令行里测试,确认能拿到数据
  4. curl 转 Python 工具 转成代码
  5. 在 Python 里集成到项目中

Curl 的好处是快速验证。不用写代码,不用起项目,一行命令就能测试接口。这种效率在调试阶段特别重要。