当前位置: 首页 > news >正文

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"
}'

http://www.jsqmd.com/news/342427/

相关文章:

  • 【计算机毕业设计案例】基于VUE框架的实时新闻推送平台新闻订阅平台(程序+文档+讲解+定制)
  • 高中阶段如何研究和学习线性表示
  • 2026年质量好的仿夯土板厂家最新推荐权威榜 - 朴素的承诺
  • 如何在iPhone上恢复已删除的笔记的综合指南
  • 汽油机水泵哪家企业售后服务好?故障响应快吗? - 品牌推荐大师
  • 【计算机毕业设计案例】基于php+vue.js流浪动物宠物领养公益网站基于php+vue的动物救助网站的设计与实现(程序+文档+讲解+定制)
  • 尿素在线浓度计行业深度解析与厂家推荐 - 品牌推荐大师1
  • 大科学装置信号采集处理解决方案
  • DeepSeek总结DuckPL:为DuckDB引入过程式编程语言
  • 上海本地生活团购代运营TOP5优选(2026版):全平台赋能商户破局,标杆服务商深度测评 - 野榜数据排行
  • PHP毕设项目:基于php+vue的动物救助网站的设计与实现(源码+文档,讲解、调试运行,定制等)
  • 郑州瑞创建材有限公司——郑州GRC挂板标杆厂家,荥阳市一体化解决方案供应商 - 朴素的承诺
  • 跟AI学一手之自定义调试函数或者类
  • 2026年 化妆刷厂家推荐排行榜:动物毛化妆刷,套刷定制,轮廓刷腮红刷粉底刷眼影刷,专业美妆工具实力解析 - 品牌企业推荐师(官方)
  • 郑州瑞创建材有限公司——立足荥阳,打造河南夯土板行业标杆品牌 - 朴素的承诺
  • PHP毕设项目:基于php+vue的高校资助管理系统的设计与实现(源码+文档,讲解、调试运行,定制等)
  • 2026中国境外券商投行机构哪家靠谱?送你一份综合评估与选择指南 - Top品牌推荐
  • DeepSeek总结的DuckDB扩展开发实战指南:从标量函数到并行表函数
  • 2026十大高清免费版权图片素材下载网站推荐 - 品牌2026
  • 2026年吸塑机厂家实力推荐榜:全自动正负压/蛋糕盒/汽车配件专用等吸塑机品牌深度解析与选购指南 - 品牌企业推荐师(官方)
  • 2026年 云安全服务商推荐榜单:云安全防护/云安全解决方案/云安全技术/企业云安全/公有云安全/私有云安全,全方位守护企业数字资产 - 品牌企业推荐师(官方)
  • 耐高温水泵采购怎么选?哪些生产企业通过了行业认证? - 品牌推荐大师
  • 耐高温水泵哪家厂家质量可靠?源头生产商有哪些推荐? - 品牌推荐大师
  • 2026年钢材批发厂家厂家最新推荐:方管销售厂、钢材市场、钢材生产厂家、镀锌方管厂家、镀锌方管生产厂家、附近方管批发选择指南 - 优质品牌商家
  • 2026设计师、美工、运营必备:十大正版商用图库网站深度评测与推荐 - 品牌2026
  • 2026年钢材厂家厂家推荐:方管批发厂推荐/钢材生产厂家/哪里有方管批发/成都方管批发/成都钢材批发/成都钢材批发市场/选择指南 - 优质品牌商家
  • 为什么海外大厂开始重新评估 Airbyte?
  • 第 1 篇 | 调度系统,不只是一个“定时器”
  • 【山海鲸实战案例】通过二维组件控制三维场景天气变化
  • 如何选择一款真正能打通研发到生产的工业AI平台?