• Post Reply Bookmark Topic Watch Topic
  • New Topic

RESTful way to change value for one field

 
David Spades
Ranch Hand
Posts: 348
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have the following object:


I have and endpoint that will update "code" field of MyObject with certain id. how do I do that RESTfully?
PUT /MyObject/<id>?code=<?>
is this RESTful?
btw, I prefer not to send whole object and just id because we only allow code attribute to be editable, all other fields are not editable.
thanks
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As I understand the convention, you are modifying an existing object so the operation is a POST.

If you are creating a totally new object the operatin is a PUT

Bill
 
Daniel Spiess
Greenhorn
Posts: 3
Google App Engine Java Objective C
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's the other way around. POST is for creation, and PUT is for update.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65530
108
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or look into PATCH.
 
Ron McLeod
Saloon Keeper
Posts: 1263
131
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One way would be to use PUT and only include the attributes that you want to change. Unspecified attributes would not be effected.

GET /manage/enb/state
{
    "admin-state": "UNLOCKED",
    "cell-state": "ACTIVE",
    "operational-state": "ENABLED"
}


PUT /manage/enb/state
{
    "admin-state": "LOCKED",
}

 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65530
108
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ron McLeod wrote:One way would be to use PUT and only include the attributes that you want to change. Unspecified attributes would not be effected [sic].

Although that breaks REST principles. A PUT should remove any properties not present in the body. Otherwise, there's no way to remove individual properties. *

Hence PATCH.





* That said, it's the way that one REST API that I maintain works.
 
Tim Holloway
Bartender
Posts: 18417
58
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Spades wrote:
is this RESTful?


Not really. ReST is supposed to be stateless. You can update a persistent object's field value, but actual server-side Java objects should not be updated. If you maintain stateful server-side Java objects then you lose one of ReST's defining characteristics: the ability to set up a whole cluster of servers all running the same set of ReST services and load-balance them by sending each incoming request to whatever server is most lightly-loaded. You cannot do that with stateful objects, since the stateful objects only reside in a single JVM. And although technically you can configure a cluster to share session objects, the overhead eats into the efficiency of ReST.
 
David Spades
Ranch Hand
Posts: 348
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Holloway wrote:
David Spades wrote:
is this RESTful?


Not really. ReST is supposed to be stateless. You can update a persistent object's field value, but actual server-side Java objects should not be updated. If you maintain stateful server-side Java objects then you lose one of ReST's defining characteristics: the ability to set up a whole cluster of servers all running the same set of ReST services and load-balance them by sending each incoming request to whatever server is most lightly-loaded. You cannot do that with stateful objects, since the stateful objects only reside in a single JVM. And although technically you can configure a cluster to share session objects, the overhead eats into the efficiency of ReST.


sorry, can you explain a bit? what are "actual server-side Java objects" and "stateful server-side Java objects" here that shouldn't be updated to maintain RESTfulness? thanks
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!