rest api 的响应是否应该包含资源的 ID?

Should response of rest api contain ID of a resource?

什么是好的做法或标准 api,服务器响应是否应该包含资源的 ID?

例如,此响应包含资源的 ID

GET /users/5
{
"user_id": 5
"first_name" : "John",
"last_name" : "Doe",
"minutes_active": 10
}

这假设资源的 ID 是在请求​​的 url 中声明的 ID

GET /users/5
{
"first_name" : "John",
"last_name" : "Doe",
"minutes_active": 10
}

this assumes that an id of a resource is one stated in the url of a request

从客户端的角度来看,URI是不透明的;如果您通过将信息放入他们期望解析的 URI 来与客户端通信,那么您就失去了情节。

如果您的 API 需要使 id 对客户端可用,则 id 应该包含在资源的表示中。

What do you mean by "URI are opaque; if you are communicating information to the client by putting it into a URI that they are expected to parse..."? It looks to me like the client created the URI, so ostensibly it already knows the ID...?

通常,REST API 客户端 遵循 links,而不是构建 URI。 HTTP 请求中没有任何内容可以告诉您客户端如何选择特定的 URI——它可能是一个书签,或者其他表示形式的 link,或者填写表格的结果。