REST API 不同级别资源详细信息的 URI 模式
REST API URI pattern for different levels of resource details
我有一个资源 "User",我们为其存储了以下详细信息:
{
"id": 123,
"name": "John Doe",
"address": {
"line1": "411, ABC street"
"city": "XYZ"
"country": "ZZZ"
}
}
我需要三个 API 来列出我的用户资源:
- API 到 return 用户 ID 列表
- API 到 return 具有 ID 和名称的用户列表。
- API 到 return 包含所有信息(即 ID、姓名、地址等)的用户列表
我应该为以上各项使用什么 URI 模式?
因为所有三个请求都是针对相同的资源,所以三个请求都应该使用相同的URL。例如:
/users
如果客户端请求不同的表示,它应该使用正确的 HTTP headerAccept
。您应该定义标识不同表示的供应商类型。例如:
要获取仅包含 ID 的表示:
GET /users
Accept: application/vnd.acme.users.ids+json
要获取包含 ID 和名称的表示:
GET /users
Accept: application/vnd.acme.users.idsandnames+json
要获得包含所有内容的表示:
GET /users
Accept: application/vnd.acme.users+json
我有一个资源 "User",我们为其存储了以下详细信息:
{
"id": 123,
"name": "John Doe",
"address": {
"line1": "411, ABC street"
"city": "XYZ"
"country": "ZZZ"
}
}
我需要三个 API 来列出我的用户资源:
- API 到 return 用户 ID 列表
- API 到 return 具有 ID 和名称的用户列表。
- API 到 return 包含所有信息(即 ID、姓名、地址等)的用户列表
我应该为以上各项使用什么 URI 模式?
因为所有三个请求都是针对相同的资源,所以三个请求都应该使用相同的URL。例如:
/users
如果客户端请求不同的表示,它应该使用正确的 HTTP headerAccept
。您应该定义标识不同表示的供应商类型。例如:
要获取仅包含 ID 的表示:
GET /users
Accept: application/vnd.acme.users.ids+json
要获取包含 ID 和名称的表示:
GET /users
Accept: application/vnd.acme.users.idsandnames+json
要获得包含所有内容的表示:
GET /users
Accept: application/vnd.acme.users+json