在 cURL 中,-X 或 --request 参数用于指定 HTTP 请求方法。虽然 cURL 会根据其他参数自动推断方法,但显式指定方法可以让请求更加明确和可控。
-X 参数基础
bash# 基本语法 curl -X METHOD URL # 常见用法 curl -X GET https://api.example.com/users curl -X POST https://api.example.com/users curl -X PUT https://api.example.com/users/1 curl -X DELETE https://api.example.com/users/1 curl -X PATCH https://api.example.com/users/1
HTTP 方法详解
GET 请求
bash# GET 请求(默认,通常不需要 -X GET) curl https://api.example.com/users # 显式指定 GET curl -X GET https://api.example.com/users # 带查询参数的 GET curl -X GET "https://api.example.com/users?page=1&limit=10" # 带请求头的 GET curl -X GET \ -H "Authorization: Bearer token123" \ https://api.example.com/users
POST 请求
bash# POST 请求(使用 -d 时自动使用 POST) curl -X POST \ -H "Content-Type: application/json" \ -d '{"name":"张三"}' \ https://api.example.com/users # 表单数据 POST curl -X POST \ -d "username=admin&password=123456" \ https://api.example.com/login # 文件上传 POST curl -X POST \ -F "file=@document.pdf" \ https://api.example.com/upload
PUT 请求
PUT 用于完整更新资源,通常需要提供资源的全部字段。
bash# 基本 PUT 请求 curl -X PUT \ -H "Content-Type: application/json" \ -H "Authorization: Bearer token123" \ -d '{"id":1,"name":"张三","email":"zhangsan@example.com","age":25}' \ https://api.example.com/users/1 # PUT 更新文件内容 curl -X PUT \ -H "Content-Type: application/octet-stream" \ --data-binary @updated-file.txt \ https://api.example.com/files/document.txt # PUT 上传文件(REST API 风格) curl -X PUT \ -T local-file.pdf \ https://api.example.com/documents/123
DELETE 请求
DELETE 用于删除资源。
bash# 基本 DELETE 请求 curl -X DELETE \ -H "Authorization: Bearer token123" \ https://api.example.com/users/1 # DELETE 带请求体(某些 API 需要) curl -X DELETE \ -H "Content-Type: application/json" \ -H "Authorization: Bearer token123" \ -d '{"reason":"inactive user"}' \ https://api.example.com/users/1 # 批量删除 curl -X DELETE \ -H "Content-Type: application/json" \ -d '{"ids":[1,2,3]}' \ https://api.example.com/users/batch
PATCH 请求
PATCH 用于部分更新资源,只需提供要修改的字段。
bash# 基本 PATCH 请求 curl -X PATCH \ -H "Content-Type: application/json" \ -H "Authorization: Bearer token123" \ -d '{"email":"newemail@example.com"}' \ https://api.example.com/users/1 # JSON Patch 格式 (RFC 6902) curl -X PATCH \ -H "Content-Type: application/json-patch+json" \ -d '[{"op":"replace","path":"/email","value":"new@example.com"}]' \ https://api.example.com/users/1 # Merge Patch 格式 (RFC 7386) curl -X PATCH \ -H "Content-Type: application/merge-patch+json" \ -d '{"email":"new@example.com","settings":{"theme":"dark"}}' \ https://api.example.com/users/1
HTTP 方法对比
| 方法 | 用途 | 幂等性 | 安全性 | 典型场景 |
|---|---|---|---|---|
| GET | 获取资源 | ✅ | ✅ | 查询数据 |
| POST | 创建资源 | ❌ | ❌ | 提交表单、创建 |
| PUT | 完整更新 | ✅ | ❌ | 替换资源 |
| PATCH | 部分更新 | ❌ | ❌ | 修改部分字段 |
| DELETE | 删除资源 | ✅ | ❌ | 删除操作 |
| HEAD | 获取元数据 | ✅ | ✅ | 检查资源存在 |
| OPTIONS | 获取支持的方法 | ✅ | ✅ | CORS 预检 |
其他 HTTP 方法
bash# HEAD 请求(仅获取响应头) curl -X HEAD https://api.example.com/users/1 # 或使用 -I curl -I https://api.example.com/users/1 # OPTIONS 请求(CORS 预检) curl -X OPTIONS \ -H "Origin: https://example.com" \ -H "Access-Control-Request-Method: POST" \ https://api.example.com/users # TRACE 请求(调试,通常被禁用) curl -X TRACE https://api.example.com/debug # CONNECT 请求(建立隧道) curl -X CONNECT http://proxy.example.com:8080
RESTful API 完整示例
bash#!/bin/bash # RESTful API 完整操作示例 API_BASE="https://api.example.com/v1" TOKEN="your_bearer_token" # 1. 创建资源 (POST) echo "Creating user..." CREATE_RESPONSE=$(curl -s -X POST \ -H "Content-Type: application/json" \ -H "Authorization: Bearer $TOKEN" \ -d '{"name":"张三","email":"zhangsan@example.com"}' \ "$API_BASE/users") USER_ID=$(echo $CREATE_RESPONSE | jq -r '.id') echo "Created user ID: $USER_ID" # 2. 获取资源 (GET) echo "Getting user..." curl -s -X GET \ -H "Authorization: Bearer $TOKEN" \ "$API_BASE/users/$USER_ID" | jq # 3. 完整更新 (PUT) echo "Updating user (PUT)..." curl -s -X PUT \ -H "Content-Type: application/json" \ -H "Authorization: Bearer $TOKEN" \ -d "{\"id\":$USER_ID,\"name\":\"张三\",\"email\":\"updated@example.com\",\"age\":26}" \ "$API_BASE/users/$USER_ID" | jq # 4. 部分更新 (PATCH) echo "Updating user (PATCH)..." curl -s -X PATCH \ -H "Content-Type: application/json" \ -H "Authorization: Bearer $TOKEN" \ -d '{"age":27}' \ "$API_BASE/users/$USER_ID" | jq # 5. 删除资源 (DELETE) echo "Deleting user..." curl -s -X DELETE \ -H "Authorization: Bearer $TOKEN" \ "$API_BASE/users/$USER_ID" echo "User deleted"
注意事项
-
自动推断方法:
- 使用
-d时,cURL 默认使用 POST - 使用
-T时,cURL 默认使用 PUT - 显式使用
-X可以覆盖默认行为
- 使用
-
幂等性考虑:
- GET、PUT、DELETE 应该是幂等的
- POST、PATCH 通常不是幂等的
-
Content-Type:
- 发送数据时必须设置正确的 Content-Type
- JSON:
application/json - 表单:
application/x-www-form-urlencoded - 文件:
multipart/form-data
-
安全性:
- GET 不应该有副作用
- DELETE 操作应该谨慎
- 敏感操作需要认证