插件窝 干货文章 Linux推荐5个命令行请求神器,我几乎天天用

Linux推荐5个命令行请求神器,我几乎天天用

example curl com https 632    来源:    2025-05-13

作为Linux重度用户,命令行请求工具确实能极大提升效率。以下是我每天高频使用的5个神器,附上核心功能和使用示例:

  1. curl (全能传输工具)
# 带JSON头POST请求
curl -X POST -H "Content-Type: application/json" -d '{"key":"value"}' https://api.example.com

# 下载文件显示进度条
curl -# -O https://example.com/largefile.zip

# 测试接口响应时间(单位秒)
curl -w "%{time_total}\n" -o /dev/null -s https://google.com

优势: 支持30+协议,HTTP/2、代理、Cookie管理一应俱全

  1. httpie (人性化HTTP客户端)
# 彩色输出+自动JSON格式化
http GET https://httpbin.org/get param==value

# 表单提交+会话保持
http -f POST https://api.example.com/login username=foo password=bar --session=user

# 下载文件并保存
http --download https://example.com/file.jpg

亮点: 比curl更直观的语法,自动语法高亮,支持插件扩展

  1. jq (JSON处理瑞士军刀)
# 提取JSON数组中的特定字段
curl -s https://api.example.com/users | jq '.[].name'

# 复杂数据转换
cat data.json | jq '.results[] | {username: .login, id}'

# 格式化压缩的JSON
echo '{"foo":"bar"}' | jq .

必杀技: 支持正则匹配、数学运算、自定义函数等高级操作

  1. xh (现代curl替代品)
# 自动跟随重定向+超时控制
xh --timeout 10 --follow https://example.com

# 多部分文件上传
xh -f POST example.com/upload file@./test.jpg

# 保存请求历史记录
xh --history --all example.com

特点: Rust编写,默认彩色输出,内置JSON支持,比curl快30%

  1. websocat (WebSocket调试利器)
# 连接WS服务器并交互
websocat wss://echo.websocket.org

# 保存WS通信记录
websocat --binary --dump-communication ws://example.com

# 压力测试模式
websocat -B 1048576 wss://example.com

场景: 实时监控WS消息流,支持TLS和二进制协议

高阶组合技示例:

# 监控API响应时间变化(每2秒)
watch -n 2 "curl -s -w '%{time_total}s\n' -o /dev/null api.example.com"

# 批量测试URL可用性
cat urls.txt | xargs -P 10 -I {} curl -s -o /dev/null -w '%{http_code} {}\n' {}

# 实时解析Nginx日志中的异常请求
tail -f /var/log/nginx/access.log | jq -R 'fromjson? | select(.status >= 500)'

这些工具的组合可以覆盖99%的日常请求场景,建议通过alias设置常用参数提升效率,例如:

alias curltime='curl -w "DNS: %{time_namelookup}s | Connect: %{time_connect}s | TTFB: %{time_starttransfer}s | Total: %{time_total}s\n" -o /dev/null -s'

需要特定场景的详细用法可以告诉我,我可以给出更针对性的方案。