curl & wget

Basic Requests

curl https://example.com              # fetch URL, print to stdout
curl -o page.html https://example.com # save to file
curl -O https://example.com/file.zip  # save with remote filename
curl -s https://example.com           # silent mode (no progress)
curl -i https://api.example.com       # include response headers
curl -I https://example.com           # HEAD request (headers only)

wget https://example.com/file.zip     # download file
wget -q https://example.com/file.zip  # quiet mode
wget -O output.zip https://example.com/file.zip  # save as custom name
wget -P /tmp/downloads https://example.com/file.zip  # save to directory

HTTP Methods

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

# POST with form data
curl -d "name=Alice&age=25" https://api.example.com/users

# POST with JSON
curl -d '{"name":"Alice","age":25}' \
     -H "Content-Type: application/json" \
     https://api.example.com/users

Headers & Auth

curl -H "Authorization: Bearer TOKEN" https://api.example.com  # bearer token
curl -H "X-Custom-Header: value" https://api.example.com       # custom header
curl -H "Accept: application/json" https://api.example.com      # accept JSON
curl -u user:pass https://api.example.com                       # basic auth
curl -u user https://api.example.com                            # prompt for password

wget --header="Authorization: Bearer TOKEN" https://api.example.com
wget --user=user --password=pass https://api.example.com        # basic auth
wget --header="Content-Type: application/json" https://api.example.com

File Upload

curl -F "file=@photo.jpg" https://api.example.com/upload         # upload file
curl -F "file=@photo.jpg;type=image/jpeg" https://api.example.com  # with MIME type
curl -F "file=@/path/to/file" -F "name=Alice" https://api.example.com  # file + fields

# multipart form
curl -F "document=@report.pdf" \
     -F "title=Annual Report" \
     -F "category=finance" \
     https://api.example.com/upload

Redirect & Retry

curl -L https://example.com               # follow redirects
curl -L --max-redirs 5 https://example.com # max redirect hops
curl --retry 3 https://example.com         # retry on failure
curl --retry-delay 2 https://example.com   # wait 2s between retries
curl --connect-timeout 10 https://example.com  # connection timeout (seconds)
curl --max-time 30 https://example.com     # total timeout (seconds)

wget --max-redirect=5 https://example.com  # max redirects
wget --tries=3 https://example.com         # retry 3 times
wget --waitretry=5 https://example.com     # wait 5s between retries
wget --timeout=30 https://example.com      # timeout (seconds)

Download & Resume

curl -C - -O https://example.com/large-file.zip  # resume download
curl -# -O https://example.com/file.zip           # progress bar

wget -c https://example.com/large-file.zip        # resume download
wget -r -np -k https://example.com/docs/          # mirror site (recursive)
wget --mirror https://example.com/                # full site mirror
wget --limit-rate=1m https://example.com/file.zip # limit download speed
wget -i urls.txt                                  # download from URL list

SSL & Proxy

curl -k https://example.com                # skip SSL verification
curl --cacert ca.pem https://example.com   # use custom CA cert
curl --cert client.pem --key key.pem https://example.com  # client cert
curl -x http://proxy:8080 https://example.com             # HTTP proxy
curl -x socks5://proxy:1080 https://example.com          # SOCKS5 proxy

wget --no-check-certificate https://example.com  # skip SSL verification
wget --ca-certificate=ca.pem https://example.com
wget -e use_proxy=yes http_proxy=http://proxy:8080 https://example.com

Response Handling

curl -w "\nHTTP Code: %{http_code}\nTime: %{time_total}s\n" \
     -o /dev/null -s https://example.com       # show timing & status

curl -D headers.txt https://example.com         # dump response headers
curl -o response.json -w "%{http_code}" https://api.example.com  # save body + status

curl -s https://api.example.com | jq '.'        # pretty print JSON
curl -s https://api.example.com | python3 -m json.tool  # pretty print JSON

wget -S https://example.com                     # print server response headers
wget --server-response https://example.com      # print response headers

Useful Patterns

# check if URL is reachable
curl -s -o /dev/null -w "%{http_code}" https://example.com

# download with authentication and resume
curl -L -C - -u user:pass -o file.zip https://example.com/file.zip

# POST JSON and pretty print response
curl -s -X POST -H "Content-Type: application/json" \
     -d '{"key":"value"}' https://api.example.com | jq '.'

# debug HTTP request
curl -v https://example.com                    # verbose output
curl --trace-ascii debug.log https://example.com  # full trace to file

# download multiple files
curl -O https://example.com/a.txt -O https://example.com/b.txt

# mirror website with wget
wget --mirror --convert-links --adjust-extension --page-requisites \
     --no-parent https://example.com/docs/

基本请求

curl https://example.com              # 获取 URL 内容,输出到终端
curl -o page.html https://example.com # 保存到指定文件
curl -O https://example.com/file.zip  # 以远程文件名保存
curl -s https://example.com           # 静默模式(不显示进度)
curl -i https://api.example.com       # 包含响应头
curl -I https://example.com           # HEAD 请求(仅获取头信息)

wget https://example.com/file.zip     # 下载文件
wget -q https://example.com/file.zip  # 安静模式
wget -O output.zip https://example.com/file.zip  # 自定义文件名保存
wget -P /tmp/downloads https://example.com/file.zip  # 保存到指定目录

HTTP 方法

curl -X POST https://api.example.com/users     # POST 请求
curl -X PUT https://api.example.com/users/1     # PUT 请求
curl -X DELETE https://api.example.com/users/1  # DELETE 请求
curl -X PATCH https://api.example.com/users/1   # PATCH 请求

# POST 表单数据
curl -d "name=Alice&age=25" https://api.example.com/users

# POST JSON 数据
curl -d '{"name":"Alice","age":25}' \
     -H "Content-Type: application/json" \
     https://api.example.com/users

请求头与认证

curl -H "Authorization: Bearer TOKEN" https://api.example.com  # Bearer 令牌
curl -H "X-Custom-Header: value" https://api.example.com       # 自定义头
curl -H "Accept: application/json" https://api.example.com      # 接受 JSON
curl -u user:pass https://api.example.com                       # 基本认证
curl -u user https://api.example.com                            # 交互式输入密码

wget --header="Authorization: Bearer TOKEN" https://api.example.com
wget --user=user --password=pass https://api.example.com        # 基本认证
wget --header="Content-Type: application/json" https://api.example.com

文件上传

curl -F "file=@photo.jpg" https://api.example.com/upload         # 上传文件
curl -F "file=@photo.jpg;type=image/jpeg" https://api.example.com  # 指定 MIME 类型
curl -F "file=@/path/to/file" -F "name=Alice" https://api.example.com  # 文件 + 字段

# 多部分表单
curl -F "document=@report.pdf" \
     -F "title=Annual Report" \
     -F "category=finance" \
     https://api.example.com/upload

重定向与重试

curl -L https://example.com               # 跟随重定向
curl -L --max-redirs 5 https://example.com # 最大重定向次数
curl --retry 3 https://example.com         # 失败重试 3 次
curl --retry-delay 2 https://example.com   # 重试间隔 2 秒
curl --connect-timeout 10 https://example.com  # 连接超时(秒)
curl --max-time 30 https://example.com     # 总超时(秒)

wget --max-redirect=5 https://example.com  # 最大重定向次数
wget --tries=3 https://example.com         # 重试 3 次
wget --waitretry=5 https://example.com     # 重试间隔 5 秒
wget --timeout=30 https://example.com      # 超时(秒)

下载与断点续传

curl -C - -O https://example.com/large-file.zip  # 断点续传
curl -# -O https://example.com/file.zip           # 显示进度条

wget -c https://example.com/large-file.zip        # 断点续传
wget -r -np -k https://example.com/docs/          # 递归下载网站
wget --mirror https://example.com/                # 完整镜像网站
wget --limit-rate=1m https://example.com/file.zip # 限制下载速度
wget -i urls.txt                                  # 从文件读取 URL 列表下载

SSL 与代理

curl -k https://example.com                # 跳过 SSL 验证
curl --cacert ca.pem https://example.com   # 使用自定义 CA 证书
curl --cert client.pem --key key.pem https://example.com  # 客户端证书
curl -x http://proxy:8080 https://example.com             # HTTP 代理
curl -x socks5://proxy:1080 https://example.com          # SOCKS5 代理

wget --no-check-certificate https://example.com  # 跳过 SSL 验证
wget --ca-certificate=ca.pem https://example.com
wget -e use_proxy=yes http_proxy=http://proxy:8080 https://example.com

响应处理

curl -w "\nHTTP Code: %{http_code}\nTime: %{time_total}s\n" \
     -o /dev/null -s https://example.com       # 显示状态码和耗时

curl -D headers.txt https://example.com         # 导出响应头到文件
curl -o response.json -w "%{http_code}" https://api.example.com  # 保存响应体 + 状态码

curl -s https://api.example.com | jq '.'        # 格式化 JSON(需 jq)
curl -s https://api.example.com | python3 -m json.tool  # 格式化 JSON

wget -S https://example.com                     # 显示服务器响应头
wget --server-response https://example.com      # 显示响应头

实用技巧

# 检查 URL 是否可访问
curl -s -o /dev/null -w "%{http_code}" https://example.com

# 带认证和断点续传下载
curl -L -C - -u user:pass -o file.zip https://example.com/file.zip

# POST JSON 并格式化响应
curl -s -X POST -H "Content-Type: application/json" \
     -d '{"key":"value"}' https://api.example.com | jq '.'

# 调试 HTTP 请求
curl -v https://example.com                    # 详细输出
curl --trace-ascii debug.log https://example.com  # 完整追踪写入文件

# 批量下载多个文件
curl -O https://example.com/a.txt -O https://example.com/b.txt

# 使用 wget 镜像网站
wget --mirror --convert-links --adjust-extension --page-requisites \
     --no-parent https://example.com/docs/