news 2026/6/10 1:49:29

Linux curl 命令用法

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
Linux curl 命令用法

curl是一个强大的命令行工具,用于传输数据,支持多种协议(HTTP、HTTPS、FTP 等)。以下是详细用法:

一、基本语法

curl [options] [URL]

二、常用选项分类

1.请求方法

# GET(默认) curl https://api.example.com/data # POST curl -X POST https://api.example.com/data curl -X POST -d "name=value" https://api.example.com # PUT curl -X PUT https://api.example.com/data # DELETE curl -X DELETE https://api.example.com/data # PATCH curl -X PATCH https://api.example.com/data

2.发送数据

# 发送表单数据 curl -d "param1=value1&param2=value2" https://example.com # 发送JSON数据 curl -H "Content-Type: application/json" -d '{"key":"value"}' https://example.com # 发送文件内容 curl -d @data.json https://example.com # 发送multipart/form-data(文件上传) curl -F "file=@/path/to/file" -F "name=test" https://example.com/upload

3.设置请求头

# 单个请求头 curl -H "Authorization: Bearer token123" https://api.example.com # 多个请求头 curl -H "Content-Type: application/json" -H "X-API-Key: abc123" https://api.example.com # 自定义User-Agent curl -A "Mozilla/5.0" https://example.com

4.处理响应

# 显示响应头 curl -i https://example.com # 包含响应头 curl -I https://example.com # 仅响应头(HEAD请求) # 保存响应到文件 curl -o output.html https://example.com curl -O https://example.com/file.zip # 使用远程文件名 # 显示详细过程 curl -v https://example.com # 显示请求/响应详细信息 curl --trace-ascii debug.txt https://example.com # 静默模式 curl -s https://example.com # 不显示进度和错误信息 curl -sS https://example.com # 静默但显示错误

5.认证相关

# 基本认证 curl -u username:password https://example.com # Bearer Token认证 curl -H "Authorization: Bearer token123" https://api.example.com # Cookie curl -b "name=value" https://example.com # 发送Cookie curl -c cookies.txt https://example.com # 保存Cookie到文件 curl -b cookies.txt https://example.com # 从文件读取Cookie

6.网络设置

# 超时设置 curl --connect-timeout 10 https://example.com # 连接超时10秒 curl --max-time 30 https://example.com # 总超时30秒 # 跟随重定向 curl -L https://example.com curl --max-redirs 5 https://example.com # 限制重定向次数 # 代理设置 curl -x http://proxy.example.com:8080 https://target.com curl --proxy-user user:pass -x http://proxy:8080 https://target.com

7.SSL/TLS设置

# 跳过证书验证(测试用) curl -k https://example.com # 指定客户端证书 curl --cert client.pem --key key.pem https://example.com # 指定CA证书 curl --cacert ca.pem https://example.com

8.下载控制

# 断点续传 curl -C - -O https://example.com/largefile.zip # 限速 curl --limit-rate 100k -O https://example.com/file.zip # 并发下载多个文件 curl -O https://example.com/file1.zip -O https://example.com/file2.zip

三、实用示例

1.API测试

# GET请求 curl "https://api.github.com/users/octocat" # POST JSON数据 curl -X POST -H "Content-Type: application/json" \ -d '{"title":"Test","body":"Content"}' \ https://api.example.com/posts # 带认证的API调用 curl -H "Authorization: token YOUR_TOKEN" \ https://api.github.com/user/repos

2.文件操作

# 上传文件 curl -F "file=@/path/to/local/file" https://example.com/upload # 下载文件并显示进度条 curl -# -O https://example.com/file.zip # 下载文件到指定目录 curl -o /tmp/file.zip https://example.com/file.zip

3.调试和测试

# 查看请求详情 curl -v -X POST -d "test=data" https://example.com # 测试响应时间 curl -w "time_total: %{time_total}\n" https://example.com # 获取HTTP状态码 curl -o /dev/null -s -w "%{http_code}\n" https://example.com

4.批量操作

# 从文件读取多个URL curl -K urls.txt # urls.txt内容示例: # url = "https://example.com/1" # output = "file1.txt" # url = "https://example.com/2" # output = "file2.txt" # 使用通配符 curl -O "https://example.com/files/file[1-3].txt"

四、高级技巧

1.格式化输出

# 使用jq处理JSON响应 curl -s https://api.example.com/data | jq '.' # 自定义输出格式 curl -w "DNS: %{time_namelookup} Connect: %{time_connect}\n" https://example.com

2.环境变量

# 使用环境变量存储敏感信息 export API_TOKEN="your_token" curl -H "Authorization: Bearer $API_TOKEN" https://api.example.com

3.脚本中使用

#!/bin/bash response=$(curl -s https://api.example.com/data) if [ $? -eq 0 ]; then echo "Success: $response" else echo "Failed" fi

五、常用变量(-w选项)

# 可用的变量: %{http_code} # HTTP状态码 %{time_total} # 总时间 %{time_connect} # 连接时间 %{time_namelookup}# DNS时间 %{size_download} # 下载字节数 %{speed_download} # 下载速度 %{url_effective} # 最终URL(跟随重定向后)

六、注意事项

  1. 安全性:不要在命令行中直接暴露密码,使用环境变量或配置文件
  2. 证书验证:生产环境不要使用-k选项
  3. 性能:大量请求时考虑使用连接复用(--keepalive-time
  4. 编码:注意URL编码,特殊字符需要转义

七、查看帮助

curl --help # 简要帮助 man curl # 完整手册 curl --manual # 详细手册

八、实战

curl -X POST \
-H "Content-Type: application/x-www-form-urlencoded" \
-H "Authorization: Bearer token" \
-d "method=c_&tags=\\A290213PT0A008840" \
http://tzy.com/mxywebapi/webapi/PSpaceQuery.ashx

curl -X POST "http://tzy.com/auth/dreamCloudReturn" \
-H "Content-Type: application/json" \
-d '{
"applyClientId": "w20thf500dom",
"jwt": "token"
}'

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/6/6 9:51:38

Gartner:AI 和数据中心支出大幅增长

据分析师Gartner最新预测,IT支出将增长近11%(10.8%),到2026年将达到6.2万亿美元。Gartner最新的IT支出预测显示,2026年数据中心设备支出将增长32%,而软件支出预计增长近15%。这很大程度上与超大规模服务商扩…

作者头像 李华
网站建设 2026/6/9 18:36:55

JAVA旅游系统源码,畅享智慧出行新体验

以下是一套基于 JAVA 的旅游系统源码解决方案,整合了智能行程规划、一站式预订、实时信息推送等核心功能,助力用户畅享智慧出行新体验: 一、技术架构 后端框架 核心框架:Spring Boot 2.7 Spring Cloud 2021.0.5(支持…

作者头像 李华
网站建设 2026/6/9 6:54:07

OpenClaw新手入门教程:OpenClaw/Clawdbot快速教程分享

OpenClaw新手入门教程:OpenClaw/Clawdbot快速教程分享。2026年OpenClaw(Clawdbot)一键部署教程。OpenClaw(原名Clawdbot/Moltbot)是一款开源的本地优先AI代理与自动化平台。它不仅能像聊天机器人一样对话,更能通过自然语言调用浏览…

作者头像 李华
网站建设 2026/6/6 22:21:14

PHP毕设选题推荐:基于php+vue的篮球馆智慧管理系统场地信息管理、预约【附源码、mysql、文档、调试+代码讲解+全bao等】

博主介绍:✌️码农一枚 ,专注于大学生项目实战开发、讲解和毕业🚢文撰写修改等。全栈领域优质创作者,博客之星、掘金/华为云/阿里云/InfoQ等平台优质作者、专注于Java、小程序技术领域和毕业项目实战 ✌️技术范围:&am…

作者头像 李华