Restful update object




















Edit: Thanks to some comments from below, I am rewording my answer. I still stand by the way I design my APIs and send back responses but I think it makes sense to qualify some of my design thoughts. Ex: if you send a create payload, I may in the backend create other entities such as UUID and maybe timestamps and some graph connections even.

I would send all this back in the response not just the request payload as is - which is pointless. I've discussed this in the comments, but what I would like to caveat is that I would try my best to design my APIs or my resources such that it doesn't have to have very large payloads. I would try to break down the resources into smaller and manageable entities such that each resource is defined by JSON attributes and not larger.

In the case that the object is very large or the parent object is being updated, then I would send back the nested structures as hrefs. Server to server APIs might think differently about this.

I am focusing on APIs that drive a user experience. Referencing to the link RFC standards , you should return created status on successfully storing the request resource using Post. In most of the applications the id of the resource is generated by the server itself, so it is good practice to return the id of the created resource.

Returning the whole object is the overhead for Post request. Ideal practice is to return the URL location of the newly created resource. For example you can refer to the following example that saves the Employee Object into the database and returns the URL of the newly created resource object as a response. More often than not it is best to return some sort of content back to the API consumer to avoid unnecessary round trips one of the reasons why GraphQL exists.

As a matter of fact, as our applications become more data-intensive and distributed, I try observe this guideline:. Now, it would be interesting if the application could parameterize the type of "content" in the response body do we want just the location of the new object, or some of the fields, or the entire object, etc. Or it could encode some sort of GraphQL query as a parameter I can see this being useful but also become a maintenance nightmare.

I would avoid this approach, though. What happens when you legitimately need to return different type of content? One endpoint per content type? Not scalable or maintainable. If true return the created resource, else don't. Then the client can decide based on their scenario.

Most of the time it's the client's application which is the determining factor of needing the resource or not. You can't possibly know a client's requirements, no matter how your API or App is configured or what scenario you think may be a time to return the resource or not.

Yes ok there will be some scenarios where for some reason created resource must be or must not be returned, etc. But for most APIs, the client being able to choose is by far the best option. It's easy: when you have created an object on the server , simply return a "handle" or "id" to the object that you have just created.

The client can refer to this "handle" to request whatever future information is needed. And of course, since the possibility exists that this "handle" will never again be referred to, provide some sort of "drop-dead timestamp" and a "garbage collection sweeper. Just returning the object is pointless. The caller has the object already. Typically that would be some Id. Sign up to join this community.

The best answers are voted up and rise to the top. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Learn more. Ask Question. Asked 5 years, 9 months ago. This is particularly bad when used in a simple example like this.

I presented a RESTful method that is more than capable of performing exactly the action you used as your example. Modify the 'email' value in the json.

I am not a REST purist. Take a look at: dev. Email is a resource and REST operates on resources. There is no need for query parameters.

Query parameters can be used to change the response. I believe this is address in Fielding's dissertation. The problem in particular is what I was considering as a resource. I was just considering an object's resource, a line in the table and not its attributes.

But please consider that the idea I was trying to convey is that we use query parameters to automate certain updates. Because sometimes there are several attributes of an object that we want to update in a particular way. Because attributes are part of the same object, having a single Class with several methods to update each attribute seems more flexible to me. And it offers us the possibility of having a single endpoint exposed to perform several different update operations on the same object.

And the query parameter was what I found most appropriate to automate these updates. But certainly this also brings some cons that should be well analyzed. Your making it too difficult. This changes all the attributes atomically. Either all changes happen status code or none of the changes happen any status code in or range. Now the tricky part is what happens when you have multiple objects that have to be modified atomically?

Consider transferring money from one account to another. What happens if the second transaction fails? How do you make that transaction atomic? One way I have seen discussed is to use a transaction endpoint. You POST:. Jan Algermissen Jan Algermissen 4, 4 4 gold badges 24 24 silver badges 38 38 bronze badges.

Something like a pure RPC method call that doesn't change the state of the object at all. What is the RESTful way of doing this? I do not understand the email URI question. Do you want to implement a gateway that you can POST to to have it send an email or are you looking for mailto:customer. REST is about manipulating resource state by transferring representations.

Whatever it is you want to achieve you do by transferring a representation to a resource with the appropriate semantics. Beware of the terms 'pure method calls' or 'business logic' as they too easily imply 'HTTP is for transport'. If you need to send an email, POST to a gateway resource, if you need to merge to accounts, create a new one and POST representations of the other two, etc.

See also how Google does it: googlecode. Show 7 more comments. You should use POST for partial updates. Maybe an order? Shouldn't POST requests not be idempotent? Surely updating an entry is idempotent and should thus be a PUT instead? And as mentioned, PUT must replace an entire resource. Show 3 more comments. Community Bot 1 1 1 silver badge. Please note that meanwhile the RFCs for json-patch and xml-patch have been finalized. Here are two solution that I can think of: do a PUT with the entire resource.

John Saunders k 25 25 gold badges silver badges bronze badges. This would be more useful if posted as a separate question. Rules : If the provided merge patch contains members that do not appear within the target, those members are added.

If the target does contain the member, the value is replaced. Voicu Voicu How do you get to the sub-resource? I refactored the answered trying to make it more clear — raspacorp. I like the way of logical grouping of fields in POST sub-resource. You can accept jObject as parameter and parse its value to update the resource. Add property. ToString , property.

Yahyaa 1 1 silver badge 12 12 bronze badges. Puneet Pathak Puneet Pathak 51 2 2 bronze badges. Regarding your Update. Max Toro Max Toro John Saunders Yes, it's an extension method. See docs. MStodd MStodd 4, 3 3 gold badges 28 28 silver badges 49 49 bronze badges. The Overflow Blog.



0コメント

  • 1000 / 1000