Skip to content

Curl 中文速查表:详尽指南

curl 是一个功能强大的命令行工具,用于通过 URL 传输数据。它支持多种协议,包括 HTTP、HTTPS、FTP 等,是开发人员和系统管理员的必备利器。本速查表旨在提供一个详尽的中文参考,涵盖 curl 的常用命令、选项及其用法。

基本用法

最基础的 curl 命令用于获取网页内容:

bash
curl https://www.example.com

此命令会向指定 URL 发出 GET 请求,并将服务器返回的内容输出到终端。

常用选项

curl 拥有众多选项,以下是一些最常用的选项及其说明:

选项 (长格式)选项 (短格式)描述示例
--request-X指定 HTTP 请求方法。curl -X POST https://example.com
--header-H添加自定义请求头。curl -H "Content-Type: application/json" https://api.example.com
--data-d发送 HTTP POST 数据。curl -d "name=value" https://example.com/form
--user-u设置服务器用户和密码进行认证。curl -u username:password https://secure.example.com
--output-o将输出写入文件,而不是标准输出。curl -o page.html https://example.com
--remote-name-O使用 URL 中的文件名保存文件。curl -O https://example.com/file.zip
--include-i在输出中包含 HTTP 响应头。curl -i https://example.com
--head-I仅获取 HTTP 头部信息。curl -I https://example.com
--location-L跟随服务器的重定向。curl -L https://short.url
--silent-s静默模式,不显示进度条或错误信息。curl -s https://api.example.com/data
--verbose-v显示详细的通信过程,用于调试。curl -v https://example.com
--cookie-b发送 cookie。curl -b "name=value" https://example.com
--cookie-jar-c将服务器发送的 cookie 保存到文件。curl -c cookies.txt https://example.com
--upload-file-T上传本地文件到目的地。curl -T localfile.txt ftp://ftp.example.com/
--form-F模拟 HTTP 表单提交,常用于文件上传。curl -F "file=@localfile.jpg" https://example.com/upload
--insecure-k忽略 SSL 证书验证。curl -k https://self-signed.example.com
--limit-rate限制传输速度。curl --limit-rate 100K https://largefile.com/file.zip
--continue-at-C断点续传失败的下载。curl -C - -O https://example.com/largefile.zip
--connect-timeout设置连接超时时间(秒)。curl --connect-timeout 10 https://example.com
--user-agent-A发送自定义的 User-Agent。curl -A "My-Custom-Browser/1.0" https://example.com

发送不同类型的请求

GET 请求

默认情况下,curl 发送 GET 请求。你也可以明确指定:

bash
curl -X GET https://api.example.com/users

POST 请求

使用 -d--data 发送 POST 请求的数据。

bash
curl -X POST -d "param1=value1&param2=value2" https://api.example.com/submit

发送 JSON 数据时,通常需要设置 Content-Type 请求头:

bash
curl -X POST -H "Content-Type: application/json" -d '{"key":"value"}' https://api.example.com/data

也可以从文件中读取数据发送:

bash
curl -d '@data.json' https://api.example.com/data```
这里 `data.json` 是包含要发送数据的文件。

#### PUT 请求
用于更新资源:

```bash
curl -X PUT -d "data to update" https://example.com/resource/1

DELETE 请求

用于删除资源:

bash
curl -X DELETE https://example.com/resource/1
  • 保存 Cookie: 从服务器接收的 Cookie 可以保存到文件中。

    bash
    curl -c cookies.txt https://example.com/login
  • 使用 Cookie: 在后续请求中使用之前保存的 Cookie。

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

文件上传和下载

  • 下载文件: 使用 -o-O 选项。

    bash
    # 将文件保存为 "downloaded_file"
    curl -o downloaded_file https://example.com/file.zip
    
    # 使用服务器上的文件名 (file.zip) 保存
    curl -O https://example.com/file.zip
  • 上传文件: 使用 -F (表单) 或 -T (FTP/SFTP)。

    bash
    # 通过 HTTP 表单上传
    curl -F "file=@/path/to/your/file.txt" https://example.com/upload
    
    # 使用 FTP 上传
    curl -T /path/to/your/file.txt ftp://user:password@ftp.example.com/

调试和输出控制

  • 详细输出 (-v): 显示包括请求头、响应头和连接信息的详细调试信息。

    bash
    curl -v https://example.com
  • 仅显示头部 (-I): 对于只需要检查响应头(如状态码、内容类型等)的场景非常有用。

    bash
    curl -I https://example.com
  • 静默模式 (-s): 在脚本中运行时,可以隐藏进度和错误信息,只输出最终结果。

    bash
    content=$(curl -s https://api.example.com/data)

Released under the MIT License.