cURL 的 -X 参数有什么作用?PUT、DELETE、PATCH 请求如何使用?
在 cURL 中,-X 或 --request 参数用于指定 HTTP 请求方法。虽然 cURL 会根据其他参数自动推断方法,但显式指定方法可以让请求更加明确和可控。-X 参数基础# 基本语法curl -X METHOD URL# 常见用法curl -X GET https://api.example.com/userscurl -X POST https://api.example.com/userscurl -X PUT https://api.example.com/users/1curl -X DELETE https://api.example.com/users/1curl -X PATCH https://api.example.com/users/1HTTP 方法详解GET 请求# GET 请求(默认,通常不需要 -X GET)curl https://api.example.com/users# 显式指定 GETcurl -X GET https://api.example.com/users# 带查询参数的 GETcurl -X GET "https://api.example.com/users?page=1&limit=10"# 带请求头的 GETcurl -X GET \ -H "Authorization: Bearer token123" \ https://api.example.com/usersPOST 请求# POST 请求(使用 -d 时自动使用 POST)curl -X POST \ -H "Content-Type: application/json" \ -d '{"name":"张三"}' \ https://api.example.com/users# 表单数据 POSTcurl -X POST \ -d "username=admin&password=123456" \ https://api.example.com/login# 文件上传 POSTcurl -X POST \ -F "file=@document.pdf" \ https://api.example.com/uploadPUT 请求PUT 用于完整更新资源,通常需要提供资源的全部字段。# 基本 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/123DELETE 请求DELETE 用于删除资源。# 基本 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/batchPATCH 请求PATCH 用于部分更新资源,只需提供要修改的字段。# 基本 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/1HTTP 方法对比| 方法 | 用途 | 幂等性 | 安全性 | 典型场景 || ------- | ------- | --- | --- | ------- || GET | 获取资源 | ✅ | ✅ | 查询数据 || POST | 创建资源 | ❌ | ❌ | 提交表单、创建 || PUT | 完整更新 | ✅ | ❌ | 替换资源 || PATCH | 部分更新 | ❌ | ❌ | 修改部分字段 || DELETE | 删除资源 | ✅ | ❌ | 删除操作 || HEAD | 获取元数据 | ✅ | ✅ | 检查资源存在 || OPTIONS | 获取支持的方法 | ✅ | ✅ | CORS 预检 |其他 HTTP 方法# HEAD 请求(仅获取响应头)curl -X HEAD https://api.example.com/users/1# 或使用 -Icurl -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:8080RESTful API 完整示例#!/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-TypeJSON: application/json表单: application/x-www-form-urlencoded文件: multipart/form-data安全性:GET 不应该有副作用DELETE 操作应该谨慎敏感操作需要认证