当前位置:首页>Linux>Linux命令每日一清单045:curl--数据传输命令速查表

Linux命令每日一清单045:curl--数据传输命令速查表

  • 2026-10-11 08:44:05
Linux命令每日一清单045:curl--数据传输命令速查表

curl是一个功能强大的命令行工具和库,用于在各种协议(包括HTTP、HTTPS、FTP、SFTP、SMTP等)之间传输数据。作为API调试和文件下载的首选工具,curl以其灵活性和广泛协议支持而闻名。

curl支持几乎所有的网络协议,能够处理各种认证方式、自定义请求头、Cookie、代理、SSL/TLS等复杂场景。无论是测试REST API、下载文件、上传数据,还是调试网络问题,curl都是不可或缺的命令行工具。掌握curl是现代开发和运维的必备技能。

1. 基本请求

命令说明
curl https://example.com
发送GET请求并输出响应体
curl -o file.html https://example.com
将响应保存到指定文件
curl -O https://example.com/file.txt
使用远程文件名保存
curl -s https://example.com
静默模式(不显示进度和错误)
curl -v https://example.com
详细输出(请求/响应头+数据)
curl -I https://example.com
仅获取响应头(HEAD请求)
curl -L https://example.com
跟随重定向
curl --head https://example.com
同-I,仅显示响应头

2. HTTP方法

命令说明
curl -X GET https://api.example.com
GET请求(默认)
curl -X POST https://api.example.com
POST请求
curl -X PUT https://api.example.com
PUT请求
curl -X DELETE https://api.example.com
DELETE请求
curl -X PATCH https://api.example.com
PATCH请求
curl -X OPTIONS https://api.example.com
OPTIONS请求(查看支持的方法)

3. POST数据

命令说明
curl -d "key=value" https://api.example.com
POST表单数据(Content-Type: application/x-www-form-urlencoded)
curl -d "name=John&age=30" https://api.example.com
多个表单字段
curl -d @data.txt https://api.example.com
从文件读取POST数据
curl --data-urlencode "name=John Doe" https://api.example.com
URL编码POST数据
curl -F "file=@/path/image.jpg" https://api.example.com
上传文件(multipart/form-data)
curl -F "field=value" -F "file=@path" https://api.example.com
表单字段+文件上传
curl -F "file=@path;filename=new.jpg" https://api.example.com
自定义上传文件名

4. JSON数据

命令说明
curl --json '{"name":"John","age":30}' https://api.example.com
POST JSON数据(curl 7.82+)
curl --json @data.json https://api.example.com
从文件POST JSON
echo '{"key":"value"}' | curl --json @- https://api.example.com
从标准输入POST JSON
curl -d '{"key":"value"}' -H "Content-Type: application/json" https://api.example.com
POST JSON(旧版curl)
curl -H "Accept: application/json" https://api.example.com
请求JSON格式响应
curl https://api.example.com | jq
解析JSON响应(需安装jq)
curl https://api.example.com | jq '.data[0].id'
提取特定字段

5. 请求头

命令说明
curl -H "X-Custom-Header: value" https://api.example.com
添加自定义请求头
curl -H "Authorization: Bearer token123" https://api.example.com
Bearer Token认证
curl -H "Content-Type: text/xml" https://api.example.com
设置内容类型
curl -A "Mozilla/5.0" https://example.com
设置User-Agent
curl -e "https://google.com" https://example.com
设置Referer
curl -H "Accept-Language: zh-CN" https://example.com
设置语言偏好
curl -H "X-Request-ID: 12345" -H "X-Client: curl" https://api.example.com
多个请求头

6. 认证

命令说明
curl -u username:password https://api.example.com
HTTP基本认证
curl -u username https://api.example.com
交互式输入密码
curl --digest -u user:pass https://api.example.com
Digest认证
curl --ntlm -u user:pass https://api.example.com
NTLM认证
curl -H "Authorization: Bearer token" https://api.example.com
Bearer Token
curl --oauth2-bearer token https://api.example.com
OAuth2 Bearer Token
curl --aws-sigv4 "aws:amz:region:service" https://api.example.com
AWS签名v4

7. Cookie管理

命令说明
curl -b "name=value" https://example.com
发送单个Cookie
curl -b "session_id=abc123; user=john" https://example.com
发送多个Cookie
curl -b cookies.txt https://example.com
从文件读取Cookie
curl -c cookies.txt https://example.com
保存响应中的Cookie到文件
curl -b cookies.txt -c cookies.txt https://example.com
读取并保存Cookie(维持会话)
curl --cookie-jar cookies.txt https://example.com
同-c,保存Cookie

8. SSL/TLS

命令说明
curl -k https://self-signed.badssl.com
忽略SSL证书错误(不推荐生产)
curl --insecure https://self-signed.badssl.com
同-k
curl --cacert ca-certificates.crt https://example.com
使用自定义CA证书
curl --cert client.crt https://api.example.com
使用客户端证书
curl --key client.key https://api.example.com
使用客户端私钥
curl --cert client.pem:password https://api.example.com
带密码的客户端证书
curl --tlsv1.2 https://example.com
指定TLS版本
curl --ciphers 'ECDHE-RSA-AES128-GCM-SHA256' https://example.com
指定加密套件

9. 代理

命令说明
curl -x proxy.example.com:8080 https://example.com
使用HTTP代理
curl --proxy proxy.example.com:8080 https://example.com
同-x
curl -x socks5://proxy.example.com:1080 https://example.com
使用SOCKS5代理
curl -x socks5h://proxy.example.com:1080 https://example.com
SOCKS5(远程DNS解析)
curl --proxy-user username:password -x proxy:8080 https://example.com
代理认证
curl --noproxy "*.local,localhost" https://example.com
绕过指定域名的代理
curl -x "" https://example.com
禁用代理

10. 下载与续传

命令说明
curl -o output.zip https://example.com/file.zip
保存到指定文件
curl -O https://example.com/file.zip
使用远程文件名保存
curl -O https://example.com/file1.zip -O https://example.com/file2.zip
下载多个文件
curl -C - -O https://example.com/largefile.zip
断点续传
curl --limit-rate 1M https://example.com/largefile.zip
限速1MB/s
curl -# https://example.com/largefile.zip
显示进度条
curl -# -o file.zip https://example.com/file.zip
进度条+指定文件名
curl --retry 3 --retry-delay 2 https://example.com
失败重试3次,间隔2秒

11. 上传文件

命令说明
curl -T file.txt https://example.com/upload/
使用PUT上传文件
curl -T file.txt ftp://example.com/
上传到FTP
curl -F "file=@/path/image.jpg" https://api.example.com/upload
表单方式上传文件
curl -F "file=@image.jpg;type=image/png" https://api.example.com
指定Content-Type
curl -F "file=@image.jpg;filename=new.jpg" https://api.example.com
自定义文件名
curl --upload-file file.txt https://example.com
使用PUT上传(同-T)

12. FTP操作

命令说明
curl ftp://ftp.example.com/file.txt
下载FTP文件
curl -T file.txt ftp://ftp.example.com/
上传文件到FTP
curl -u user:pass ftp://ftp.example.com/
FTP认证访问
curl --ftp-ssl ftp://ftp.example.com/
FTP over SSL(FTPS)
curl ftp://ftp.example.com/ --list-only
列出FTP目录内容
curl ftp://ftp.example.com/ -o file.txt
下载并重命名

13. 超时控制

选项说明
--connect-timeout 10
连接超时(秒)
-m 30
 或 --max-time 30
最大总传输时间(秒)
--retry 3
失败时重试次数
--retry-delay 5
重试间隔(秒)
--retry-max-time 30
最大重试总时间(秒)
--speed-limit 1000
速度低于此值时触发超时(字节/秒)
--speed-time 10
速度超时触发时间(秒)

14. 输出控制

选项说明
-s
静默模式(不显示进度和错误)
-S
静默模式下显示错误(与-s配合)
-w "%{http_code}"
输出HTTP状态码
-w "%{time_total}\n"
输出总耗时(秒)
-w "HTTP %{http_code} Time %{time_total}\n"
自定义输出格式
-o /dev/null -s -w "%{http_code}"
仅获取状态码
-D headers.txt
将响应头保存到文件
--write-out '%{url_effective}'
输出最终URL(跟随重定向后)

15. 调试

选项说明
-v
详细输出(请求和响应头)
-vv
更详细的输出(包含协议细节)
--trace trace.txt
将所有数据写入文件
--trace-ascii ascii.txt
以ASCII格式写入跟踪文件
--trace-time
跟踪输出中显示时间戳
--libcurl libcurl.c
生成等效的libcurl代码

16. 实用组合模式

命令说明
curl -fsSL https://example.com/script.sh | bash
静默失败、跟随重定向、执行脚本
curl -u token: -H "X-API-Key: key" https://api.example.com
API Token认证
curl -X POST -d @data.json -H "Content-Type: application/json" https://api.example.com
从文件POST JSON
curl -k -L -H "Authorization: Bearer $TOKEN" https://api.example.com
忽略SSL+跟随重定向+Bearer认证
curl -s https://api.example.com | jq -r '.items[].id'
提取JSON数组中的ID
curl -w "@curl-format.txt" -o /dev/null -s https://example.com
从文件读取输出格式
curl -X POST -F "file=@/path/file" -H "X-API-Key: key" https://upload.example.com
带认证的文件上传
curl -H "If-Modified-Since: $(date -R)" https://example.com
条件请求

17. 故障排查

问题解决方法
curl: (7) Failed to connect
检查网络连接;确认URL和端口正确;检查防火墙
curl: (35) SSL connect error
SSL/TLS握手失败;使用-k跳过验证(临时);检查证书
curl: (22) The requested URL returned error: 404
URL路径错误;检查API端点
curl: (52) Empty reply from server
服务器无响应;检查服务器配置
curl: (28) Operation timed out
增加超时时间:--connect-timeout 30;检查网络延迟
JSON数据未正确解析
确认Content-Type: application/json;检查JSON格式
认证失败
检查认证方式(Basic/Bearer/OAuth);确认凭据正确
重定向循环
使用-L跟随重定向;检查重定向配置
SSL证书过期
更新CA证书:update-ca-certificates;临时使用-k

温馨提示: curl是API开发和调试的首选工具。建议掌握以下技巧:1)开发API时先用-v查看请求/响应头;2)使用-o /dev/null -s -w组合测试响应时间;3)JSON API优先使用--json(新版本)或-H "Content-Type: application/json" -d @file;4)处理敏感信息时使用.netrc文件或环境变量存储凭据;5)复杂请求可先保存到文件,用--config执行。在脚本中使用curl时,建议设置--fail(-f)使非200状态码返回非零退出码,便于错误处理。对于需要保持会话的请求,使用-c和-b管理Cookie。调试SSL问题时,--trace-ascii可以提供详细的握手信息。

本文PDF格式下载:

  关注公众号(haopython),请回复: LTCURL

最新文章

随机文章