Question

Suppose I have a complex object from html page which is mapped to this JSON structure:

{
    id:"", //is not shown to user
    title : "",
    description: "",
    summary: "",
    // other too many fields
}

To update this record with "common" REST approach I should use:

- POST rest/record/{id}

With "common" approach entirely record object is marshalled to JSON object and is passed to REST service, then this entirely object is validated, passed to SQL query to data base and DB engine updates a record with all data. But what if user just update one symbol in the title?

In that case I should split this object into several:

{
    id:"", //is not shown to user
    { recordId:"", title : "",         } ,
    { recordId:"", description: "",    } ,
    { recordId:"", summary: "",        } ,
    // other too many fields
}

How I should reorganize rest URLs? Like that:

- POST rest/record/{id}/title
- POST rest/record/{id}/description
- POST rest/record/{id}/summary
- others

Is this approach with URL good or bad (I mean both for javaScript from end and REST back end programming)? Is there any other approaches to handle this problem?

Was it helpful?

Solution

Instead of using POST, use PATCH and send only what's been changed:

PATCH rest/record/{id}

Data: { title: "new title" }

URLs such as rest/record/{id}/title, rest/record/{id}/summary, etc. aren't really RESTfull since they are not resources but properties of a resource.

OTHER TIPS

See this past question for some exploration of options here (include PATCH).

Best practice for partial updates in a RESTful service

You have the following options:

  • use PATCH and send only the title
  • use PUT and send the whole data again
  • use PUT and use the property as a sub-resource with url: /resource/title

(POST is not idempotent, so you should not use that for update)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top