We are using Golang to implement a REST API which including CRUD, in Update service, client could send partial JSON including changed fields, and we need to handle for updating entity with these changes.
Logically, we need to get entity by Id from DB to struct, and then unmarshal payload json to another struct and update entity.
However if payload json is not fully, for example I have struct
type Customer struct { Id int64 `json:"id"` Name string `json:"name"` Age int `json:"age"` } And JSON request looks like
{ "Name": "Updated name" } Then the customer should be updated with new name.
That's simple example, actually it could be a nested struct and nested json, how could we handle that case with golang, or event other language like Java, .NET
PATCHto perform a partial update or update a resource viaPUTthat partially overlap the actual resource. To the actual resource this has the effect of a partial update then, though the semantics ofPUTremain: replace the current payload of the targeted resource with the payload provided in the request. Anything other is a violation of the HTTP protocol. Also, patching should send instructions to the server on how to modify the resource to end up in a desired state.PATCHing the resource with media-typeapplication/merge-patch+jsonas spedified in RFC 7396 and only for such media-types. I'd still recommend to useapplication/json-patch+jsonas specified in RFC 6902 though