PATCH方法是否应在响应正文中返回资源的所有字段? [英] Should the PATCH method return all fields of the resource in the response body?
问题描述
PATCH方法是否应在响应正文中返回资源的所有字段?
还是只返回更新的字段?
Should the PATCH method return all fields of the resource in the response body?
Or should it return only updated fields?
我正在阅读此
例如,如果它仅返回更新的字段,则用户可以知道服务器中哪些字段已更新,而用户则更新了某些字段.
For example, if it returns only updated fields, the user could know which fields were updated in the server, while the user updated some fields.
**Users resource representations**
name: string
age: number
createdon: date
modifiedon: date
PATCH /users/{userId}
Request body
{
name: 'changedname',
}
Response body Case1
{
name: 'changedname',
age: 20,
createdon: 2016-01-01,
modifiedon: 2016-06-09
}
Response body Case2
{
name: 'changedname',
modifiedon: 2016-06-09
}
推荐答案
通常应通过内容协商来解决.换句话说,客户询问用于特定表示(如果需要).该请求将如下所示:
Normally this should be handled through content negotiation. In other words, the client asks for a specific representation if it needs one. The request would look like this:
PATCH /user/123
Content-Type: application/merge-patch+json
Accept: application/vnd.company.user+json
...
在这种情况下,客户端表示希望使用完整的user
表示作为答案.还是可以的:
In this case, the client expresses that it wants a full user
representation as answer. Or it could do:
PATCH /user/123
Content-Type: application/merge-patch+json
Accept: application/vnd.company.object-fragment+json
...
要求某个对象的通用片段表示.
to ask for a generic fragment representation of some object.
如果您不想这样做,则不必同时实现这两种情况,在这种情况下,您只需做用例,并用406 Not Acceptable
响应您暂时不支持的media-types
.
You don't have to implement both if you don't want to, in which case you just do your use-case and respond with 406 Not Acceptable
to media-types
you do not support for the moment.
这篇关于PATCH方法是否应在响应正文中返回资源的所有字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!