微算力 · API 导航与自营网关
API 接入 EN
未登录登录
✨

HTTP 请求与 API 测试权威指南

Curl Http · v1.0.0
专业技能 安全 S 级 作者 arnarsson 公开目录收录
下载 5.6k 安装 6 收藏 13k 浏览 21
原始页面

简介

curl 官方文档封装,涵盖 HTTP 请求、API 测试、文件传输等核心用法,T1 级可信来源

原始描述

Essential curl commands for HTTP requests, API testing, and file transfers.

中文使用说明 来自收录源

核心用法

curl-http 是一款纯文档类技能,系统整理了 curl 命令行工具在 HTTP 客户端场景下的完整用法。内容涵盖六大模块:基础请求(GET/POST/PUT/PATCH/DELETE)、Header 与认证(Basic Auth、Bearer Token、API Key)、高级功能(超时重试、Cookie 管理、代理配置、SSL/TLS 选项)、响应处理(格式化输出、JSON 管道、性能计时)、文件操作(断点续传、批量下载、FTP/HTTP 上传)以及调试测试(verbose 模式、请求追踪)。

显著优点

  • 权威来源:内容基于 curl 官方文档,命令经过实际验证,覆盖 20+ 年工业级 HTTP 工具的标准用法
  • 即查即用:每个代码块均可直接复制执行,附带 jq 管道示例实现 JSON 处理
  • 场景全面:从简单网页抓取到复杂 API 测试、从开发调试到生产脚本,覆盖完整生命周期
  • 零依赖风险:纯 Markdown 文档,无可执行代码,无第三方依赖

潜在局限性

  • 无交互式引导:需用户具备基础命令行知识,新手可能需要配合外部教程学习
  • 示例域名限制:所有 URL 使用 example.com 占位符,实际使用时需替换为真实端点
  • 无内置验证:不检测用户输入的命令合法性,错误参数可能导致意外行为
  • 平台差异:部分 SSL/TLS 选项在不同系统(macOS vs Linux)的 curl 版本中行为略有差异

适合人群

  • 后端开发者进行 REST API 快速测试
  • DevOps/SRE 编写监控脚本与自动化工作流
  • 安全研究员进行 HTTP 流量分析与调试
  • 技术写作者参考标准命令格式

常规风险

  • 证书验证绕过:文档包含 -k/--insecure 示例,生产环境误用可能导致中间人攻击
  • 敏感信息暴露:命令行历史可能记录 -u 密码或 -H Token,建议配合 read -s 或环境变量使用
  • 重定向风险:-L 跟随重定向时可能跳转到恶意域名,需配合 --max-redirs 限制
  • 本地文件覆盖:-O 自动使用远程文件名,存在同名文件被覆盖风险

技能正文


name: curl-http description: Essential curl commands for HTTP requests, API testing, and file transfers. homepage: https://curl.se/ metadata: {"clawdbot":{"emoji":"🌐","requires":{"bins":["curl"]}}}

curl - HTTP Client

Command-line tool for making HTTP requests and transferring data.

Basic Requests

GET requests

# Simple GET request
curl https://api.example.com

# Save output to file
curl https://example.com -o output.html
curl https://example.com/file.zip -O  # Use remote filename

# Follow redirects
curl -L https://example.com

# Show response headers
curl -i https://example.com

# Show only headers
curl -I https://example.com

# Verbose output (debugging)
curl -v https://example.com

POST requests

# POST with data
curl -X POST https://api.example.com/users \
  -d "name=John&email=john@example.com"

# POST JSON data
curl -X POST https://api.example.com/users \
  -H "Content-Type: application/json" \
  -d '{"name":"John","email":"john@example.com"}'

# POST from file
curl -X POST https://api.example.com/users \
  -H "Content-Type: application/json" \
  -d @data.json

# Form upload
curl -X POST https://api.example.com/upload \
  -F "file=@document.pdf" \
  -F "description=My document"

Other HTTP methods

# PUT request
curl -X PUT https://api.example.com/users/1 \
  -H "Content-Type: application/json" \
  -d '{"name":"Jane"}'

# DELETE request
curl -X DELETE https://api.example.com/users/1

# PATCH request
curl -X PATCH https://api.example.com/users/1 \
  -H "Content-Type: application/json" \
  -d '{"email":"newemail@example.com"}'

Headers & Authentication

Custom headers

# Add custom header
curl -H "User-Agent: MyApp/1.0" https://example.com

# Multiple headers
curl -H "Accept: application/json" \
     -H "Authorization: Bearer token123" \
     https://api.example.com

Authentication

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

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

# API key in header
curl -H "X-API-Key: your_api_key" \
  https://api.example.com

# API key in URL
curl "https://api.example.com?api_key=your_key"

Advanced Features

Timeouts & retries

# Connection timeout (seconds)
curl --connect-timeout 10 https://example.com

# Max time for entire operation
curl --max-time 30 https://example.com

# Retry on failure
curl --retry 3 https://example.com

# Retry delay
curl --retry 3 --retry-delay 5 https://example.com

Cookies

# Send cookies
curl -b "session=abc123" https://example.com

# Save cookies to file
curl -c cookies.txt https://example.com

# Load cookies from file
curl -b cookies.txt https://example.com

# Both save and load
curl -b cookies.txt -c cookies.txt https://example.com

Proxy

# Use HTTP proxy
curl -x http://proxy.example.com:8080 https://api.example.com

# With proxy authentication
curl -x http://proxy:8080 -U user:pass https://api.example.com

# SOCKS proxy
curl --socks5 127.0.0.1:1080 https://api.example.com

SSL/TLS

# Ignore SSL certificate errors (not recommended for production)
curl -k https://self-signed.example.com

# Use specific SSL version
curl --tlsv1.2 https://example.com

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

# Show SSL handshake details
curl -v https://example.com 2>&1 | grep -i ssl

Response Handling

Output formatting

# Silent mode (no progress bar)
curl -s https://api.example.com

# Show only HTTP status code
curl -s -o /dev/null -w "%{http_code}" https://example.com

# Custom output format
curl -w "\nTime: %{time_total}s\nStatus: %{http_code}\n" \
  https://example.com

# Pretty print JSON (with jq)
curl -s https://api.example.com | jq '.'

Range requests

# Download specific byte range
curl -r 0-1000 https://example.com/large-file.zip

# Resume download
curl -C - -O https://example.com/large-file.zip

File Operations

Downloading files

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

# Download with custom name
curl -o myfile.zip https://example.com/file.zip

# Download multiple files
curl -O https://example.com/file1.zip \
     -O https://example.com/file2.zip

# Resume interrupted download
curl -C - -O https://example.com/large-file.zip

Uploading files

# FTP upload
curl -T file.txt ftp://ftp.example.com/upload/

# HTTP PUT upload
curl -T file.txt https://example.com/upload

# Form file upload
curl -F "file=@document.pdf" https://example.com/upload

Testing & Debugging

API testing

# Test REST API
curl -X GET https://api.example.com/users
curl -X GET https://api.example.com/users/1
curl -X POST https://api.example.com/users -d @user.json
curl -X PUT https://api.example.com/users/1 -d @updated.json
curl -X DELETE https://api.example.com/users/1

# Test with verbose output
curl -v -X POST https://api.example.com/login \
  -H "Content-Type: application/json" \
  -d '{"username":"test","password":"pass"}'

Performance testing

# Measure request time
curl -w "Total time: %{time_total}s\n" https://example.com

# Detailed timing
curl -w "\nDNS: %{time_namelookup}s\nConnect: %{time_connect}s\nTLS: %{time_appconnect}s\nTransfer: %{time_starttransfer}s\nTotal: %{time_total}s\n" \
  -o /dev/null -s https://example.com

Common debugging

# Show request and response headers
curl -v https://api.example.com

# Trace request
curl --trace-ascii trace.txt https://api.example.com

# Include response headers in output
curl -i https://api.example.com

Common Patterns

Quick JSON API test:

curl -s https://api.github.com/users/octocat | jq '{name, bio, followers}'

Download with progress bar:

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

POST JSON and extract field:

curl -s -X POST https://api.example.com/login \
  -H "Content-Type: application/json" \
  -d '{"user":"test","pass":"secret"}' | jq -r '.token'

Check if URL is accessible:

if curl -s --head --fail https://example.com > /dev/null; then
  echo "Site is up"
else
  echo "Site is down"
fi

Parallel downloads:

for i in {1..10}; do
  curl -O https://example.com/file$i.jpg &
done
wait

Useful Flags

  • -X: HTTP method (GET, POST, PUT, DELETE, etc.)
  • -d: Data to send (POST/PUT)
  • -H: Custom header
  • -o: Output file
  • -O: Save with remote filename
  • -L: Follow redirects
  • -i: Include headers in output
  • -I: Headers only
  • -v: Verbose output
  • -s: Silent mode
  • -S: Show errors even in silent mode
  • -f: Fail silently on HTTP errors
  • -k: Insecure (ignore SSL)
  • -u: Basic authentication
  • -F: Multipart form data
  • -b: Send cookies
  • -c: Save cookies
  • -w: Custom output format

Tips

  • Use -s in scripts to suppress progress bar
  • Combine -sS for silent but show errors
  • Use -L for redirects (e.g., shortened URLs)
  • Add -v for debugging
  • Use jq to process JSON responses
  • Save common requests as shell aliases or scripts
  • Use --config for complex reusable requests

Documentation

Official docs: https://curl.se/docs/ Manual: man curl HTTP methods: https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods

正文来自原始发布页, 版权归作者所有,许可未标注。 权利人认为有不妥,可申请下架。

其他来源收录

同一技能在不同目录的原始页面:

标签

http-clientapi-testingcurlcommand-linenetworkingrest-apifile-transfer

同类 Skill