CURL 命令的用法详解

CURL 命令的用法详解

CURL(Client URL)是一个强大的命令行工具,用于通过各种网络协议与服务器进行数据传输。它支持 HTTP、HTTPS、FTP、SFTP 等多种协议,广泛应用于 API 测试、数据获取、文件传输等场景。以下是 CURL 命令的详细用法:

一、基本语法

curl [选项] [URL]

选项:控制 CURL 的行为(如请求方法、头部信息、认证等)。

URL:目标服务器的地址(需包含协议,如http://example.com)。

二、核心功能与常用选项

1. 发送 GET 请求(默认方式)

# 最简单的GET请求

curl https://example.com

# 获取详细响应信息(包括头部)

curl -v https://example.com

# 仅获取响应头部

curl -I https://example.com

# 保存响应内容到文件

curl -o response.html https://example.com

2. 发送 POST 请求(提交数据)

# 提交表单数据(表单格式)

curl -X POST https://example.com/api/submit \

--data "name=John&age=30"

# 提交JSON数据(需指定Content-Type)

curl -X POST https://example.com/api/data \

-H "Content-Type: application/json" \

-d '{"key": "value", "array": [1, 2, 3]}'

# 从文件读取数据并提交

curl -X POST https://example.com/upload \

-T data.json

3. 请求头部与认证

# 添加自定义请求头

curl -H "User-Agent: MyApp/1.0" https://example.com

# 基本认证(用户名:密码)

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

# Bearer Token认证

curl -H "Authorization: Bearer YOUR_TOKEN" https://example.com/api

# 发送Cookie

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

4. 代理与网络控制

# 使用代理服务器

curl -x proxy.example.com:8080 https://example.com

# 设置超时时间(秒)

curl -m 10 https://example.com

# 限制带宽(KB/s)

curl --limit-rate 100 https://example.com/large-file

5. 文件上传与下载

# 下载文件并显示进度

curl -# -O https://example.com/file.zip

# 断点续传(已下载文件可继续下载)

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

# 上传文件到服务器(PUT方法)

curl -X PUT https://example.com/upload \

-T local-file.txt

6. 证书与 HTTPS 选项

# 忽略HTTPS证书验证(不安全,仅测试用)

curl -k https://example.com

# 指定客户端证书

curl --cert client.crt --key client.key https://example.com

# 信任CA证书文件

curl --cacert ca.crt https://example.com

三、高级用法与场景示例

1. 模拟浏览器行为

# 模拟Chrome浏览器请求

curl -H "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36" https://example.com

# 跟踪重定向(默认跟随3次)

curl -L https://example.com/redirect

2. API 测试与调试

# 测试DELETE API

curl -X DELETE https://example.com/api/item/123 \

-H "Authorization: Bearer TOKEN"

# 查看请求与响应的详细日志

curl -v https://example.com/api/endpoint

3. 批量请求与脚本化

# 从文本文件读取URL列表并批量请求

cat urls.txt | while read url; do curl -s $url | head -n 10; done

# 在Shell脚本中使用CURL

#!/bin/bash

URL="https://api.example.com/data"

TOKEN="your_token_here"

response=$(curl -s -H "Authorization: Bearer $TOKEN" $URL)

echo "Response: $response"

四、常用选项速查表

选项描述

-v / --verbose

显示详细的通信过程(调试用)

-X / --request

指定请求方法(GET/POST/PUT/DELETE 等)

-d / --data

发送 POST 数据(表单格式)

-H / --header

添加自定义请求头

-u / --user

基本认证(用户名:密码)

-o / --output

保存响应到文件

-O / --remote-name

按远程文件名保存响应

-k / --insecure

忽略 HTTPS 证书验证

-x / --proxy

使用代理服务器

-m / --max-time

设置最大请求时间(秒)

五、注意事项

URL 编码:如果参数包含特殊字符(如空格、&、=),需用%20等编码替换,或用单引号包裹参数(如'name=John Doe')。

安全性:避免在命令中明文写入密码,可通过环境变量(如CURL_USER_PASSWORD)或配置文件传递认证信息。

Windows 系统:部分选项语法需调整(如双引号替代单引号),建议使用 PowerShell 或 Git Bash 运行 CURL。

通过灵活组合上述选项,CURL 可以满足几乎所有网络请求场景。如需更详细的帮助,可运行curl --help或查看官方文档。

猜你喜欢 💖

科举1300年,高考71届,这2字弥久愈新,价值千金
华为强制刷机按哪几个键
5443655

华为强制刷机按哪几个键

📅 08-24 👁️ 3663
qq空间怎么屏蔽
365会被黑吗

qq空间怎么屏蔽

📅 07-18 👁️ 8246