How to GET a read-only vs editable resource in REST style?

I'm fairly familiar with REST principles, and have read the relevant dissertation, Wikipedia entry, a bunch of blog posts and StackOverflow questions on the subject, but still haven't found a straightforward answer to a common case:

I need to request a resource to display. Depending on the resource's state, I need to render either a read-only or an editable representation. In both cases, I need to GET the resource. How do I construct a URL to get the read-only or editable version?

If my user follows a link to GET /resource/<id>, that should suffice to indicate to me that s/he needs the read-only representation. But if I need to server up an editable form, what does that URL look like? GET /resource/<id>/edit is obvious, but it contains a verb in the URL. Changing that to GET /resource/<id>/editable solves that problem, but at a seemingly superficial level. Is that all there is to it -- change verbs to adjectives?

If instead I use POST to retrieve the editable version, then how do I distinguish between the POST that initially retrieves it, vs the POST that saves it? My (weak) excuse for using POST would be that retrieving an editable version would cause a change of state on the server: locking the resource. But that only holds if my requirements are to implement such a lock, which is not always the case. PUT fails for the same reason, plus PUT is not enabled by default on the Web servers I'm running, so there are practical reasons not to use it (and DELETE).

Note that even in the editable state, I haven't made any changes yet; presumably when I submit the resource to the Web server again, I'd POST it. But to get something that I can later POST, the server has to first serve up a particular representation.

I guess another approach would be to have separate resources at the collection level: GET /read-only/resource/<id> and GET /editable/resource/<id> or GET /resource/read-only/<id> and GET /resource/editable/<id> ... but that looks pretty ugly to me.

Thoughts?

-------------Problems Reply------------

1) It is perfectly valid to have two distinct resources, one for viewing and one for editing some domain concept. Just be aware that because they are two different URIs from REST's perspective they are two different resources. Too often people conflate resource with domain object. That's why they end up being stuck only doing CRUD.

2) Don't get too hung up on the name of the resource. The important thing is that you realize that what the URI points to is a "thing", "a resource". If that's more obvious to you with editable instead of edit then use that. Having a verb in your URL doesn't make your application wrong, it just makes it a bit less readable to the human developer. Using a verb in the URL to try and redefine the semantics of the HTTP method, now that's a violation of the uniform interface constraint.

In REST, editing an existing resource is accomplished by a client GET-ing a representation of that resource, making changes to the representation, and then doing a PUT of the new representation back to the server.

So to just read a resource your REST client program would do a:

GET http://www.example.com/SomeResource

And to edit that resource:

GET http://www.example.com/SomeResource
... edit it ...
PUT http://www.example.com/SomeResource

Normally simultaneous updates are handled by letting the last PUT arriving at the server overwrite the earlier ones, on the assumption that it represents a newer state. But in your case you want to guard against this.

Carefully consider @Jason's suggestion to maintain an optional parallel lock resource for each main resource. Your client would first create the lock, do the edit, then delete the lock. Your system would need to release a lock automatically if the user making the lock subsequently never saves any changes. This would look like:

GET http://www.example.com/SomeResource
... user presses an edit button ...
PUT http://www.example.com/SomeResource/lock
... user edits the resource's representation ...
PUT http://www.example.com/SomeResource
DELETE http://www.example.com/SomeResource/lock

You'd need to do some appropriate error handling if the user is trying to edit a resource that's locked by someone else.

It sounds like you feel you're constrained by the current limitations of HTML. If you use a server-side REST framework like Restlet (for Java), it supports the notion of "overloaded POST", where you can use POST but tack on a query string argument like method=PUT or method=DELETE. If you're writing your own server-side components they can use this trick too.

There are tricks you can play at the HTML level too. For instance your page can have a read-only part that's initially displayed, and an input form that's initially not shown. When the user presses the edit button, your JavaScript hides the read-only part and shows the input form.

Be sure to read Richardson and Ruby's Restful Web Services (O'Reilly) too. It's extremely helpful.

How do I construct a URL to get the read-only or editable version?

There's an underlying problem here, which is that you are constructing URLs in the first place - appending IDs to hard-coded URLs is not REST. Roy Fielding has written about this very mistake. Whichever document prompts you to edit the resource should contain the URI to the editable variant of that resource. You follow that URI, whether that's /resource/editable or /editable/resource is outside the scope of REST.

I don't think returning a form or just values is up to a REST server, but the responsibility of the client. Whether a resource is editable is a property of the resource, and not something defined by the URL.

In other words: The URL for getting the resource is GET /resource/<id>. This has a property editable. If a user wants a form it can retrieve the resource from the same URL and populate the form. The client can than PUT/POST changes.

If instead I use POST to retrieve the editable version, then how do I distinguish between the POST that initially retrieves it, vs the POST that saves it?

You perform a GET (not a POST) to read the resource, and POST (or PUT) to write the resource.

If you want to create a lock on the resource in question, use POST to write to the resource (or the resource's container, with the resource ID encoded in the body of the POST), and have the server create a lock as a new resource, and return an ID of that resource as the response to the POST. (with authentication issues beyond the scope of your question or this answer)

Then to unlock the lock, either use a DELETE on the lock resource, or POST to the lock's container.

I guess your question could be "how to identify the readonly representation that return with GET action in PUT action?". You could do this:

<Root>
<readonly>
<p1><p1>
...
<readonly>
<others>
...
<others>
<Root>

After parsing the request XML from PUT you can ignore the readonly part and process others. In Response, return 200 status and leave a message saying the part in readonly is ignored. Is it your expected?

Category:url Views:0 Time:2011-01-12
Tags: rest url

Related post

  • editing resource of another C# application 2011-06-04

    I have two complied C# assemblies, however I'm wanting one to be able to add string resources to the second one while the second exe is not running. Sorry this question is so brief but I'm not too sure where to even start here. Edit: Sorry, just to a

  • Runtime-editable resource file 2009-05-27

    I've created a custom XML based Error Message framework for .NET. The XML is key-value based and this gets cached. The cache is invalidated when the user edits the XML file. When an error occurs, a call like 'GetError(Password.Toolong,"Default value"

  • asp.net: edit resource file which is compiled into a dll 2010-06-29

    my asp.net site uses resource files for a referenced project with the user controls in it. so i have one website project, and one dll project with usercontrols + the resource files. The website references the dll project. So far so good. When i publi

  • Editable resource files in a deployed ASP.Net site 2011-02-04

    Apparently, my understanding of .resx files was flawed - I had assumed (without testing) that they were deployed as XML so as to remain editable at a later time - clearly, this is incorrect. So, I'm left with a gap I need to fill for the labels and f

  • Formtastic checkboxes not getting checked when editing resource, when using MongoMapper 2011-04-01

    With the following Store and Service models, managed with MongoMapper: class Store include MongoMapper::Document key :service_ids, Array, :typecast => 'ObjectId' many :services, :in => :service_ids end class Service include MongoMapper::Documen

  • How to detect Edit Resource file programatically at run time? 2011-04-20

    if resource file in a folder then we can edit this like XmlDocument loResource = new XmlDocument(); loResource.Load(Server.MapPath("/App_GlobalResources/TDLResources.de-DE.resx")); XmlNode loRoot = loResource.SelectSingleNode("root/data[@name='Requir

  • Maven: Any way to edit resource files? 2011-06-22

    I have a mavenized sql project, which has a few SQL scripts. These scripts are basically Oracle statements of the form insert xyz into some_table; show errors These files are to be copied to a deploy project, but for the statements to work, some modi

  • How to display keyboard for editing of 'right detail' style UITableViewCell? (iPhone/iPad) 2012-01-15

    I'm using storyboard to create a grouped UITableView. Within the table I have several different cells setup with the 'right detail' style. I have also added IBOutlets to the UILabels on the right of these cells, however when someone presses the cell

  • iOS - Editable Forms (Contact App style) 2012-01-24

    I'm in the process of creating an iOS5 application, what I'm after is the 'Edit' button functionality, specifically, taking a dynamically populated plain style TableView, and when the Edit button in the NavBar is pressed, it changes the form into an

  • Editing resources in Microsoft Project VBA not reflected on server 2012-02-23

    when I try to edit an enterprise resource in Microsoft Project using VBA (SetField method of resource), I am able to set custom fields, and can see those custom field changes with GetField, but these changes are not seen on the server/in PWA. And whe

  • How do I edit in-built Background Styles? 2012-10-30

    Are the in-built Background Styles editable? When I select a Background Style and then Select > Format Background > insert a Background Picture, the "Background Style" tombstone in the dropdown menu is no longer highlighted / selected. Is this

  • Bulk edit resources including "Earliest available" 2014-03-31

    I update the information we hold on resources by slecting a number and then opening in MS Project. By then adding various Enterprise fields I can copy the data into Excel and then paste back into MS Project. Firstly - is there a better way of doing b

  • In Outlook 2010, I cannot edit the calendar daily style. 2014-04-04

    I have Outlook 2010, Windows 7. I want to print my calendar for 1 day, without the list of tasks (like I used to in Outlook 2007). I open the day I want to print, click on File, Print, Print Options, Define styles. The Define Print Style box opens. I

  • Editing resource forks on the command line on Mac OS X 2008-11-22

    Mac OS X stores some files with resource forks. I need to create a file with a resource fork. The trouble is, I need to create this file on the command line. Is anyone aware of how you can create a file with a resource form on the command line in Mac

  • Is there a tool for non-developers (translators!) to edit resource bundles? 2009-06-17

    What we do in our company to let our software be translated by external translators is ... well .. how to put it... cvs co someModule ant translation.export (behind this target there is a self-made ant task, exporting all resource bundles together as

  • whats the best way to allow users to edit a site wiki style 2010-03-01

    and have the changes save straight to the mysql db only requiring an email address to make the change --------------Solutions------------- MediaWiki? It's a PHP app most commonly used with MySQL, and you can configure it to require (only) an email.

  • Edit resource files and search string in .cs files. 2010-07-31

    How can I automate searching for strings in all .cs files and add certain code for localization, where I can use a key in resource files. Let's say there is a string s = "A" in cs files. I need to change it to something like, string s = ("A","Resourc

  • Wpf - Edit Resource in run time 2010-12-01

    I have this xaml: <Grid x:Name="root"> <Grid.Resources> <Style x:Key="btnStyle"> <Setter Property="Button.Background" Value="LightBlue"/> </Style> </Grid.Resources> <Button Style="{DynamicResource btnStyle}">

  • edit resource file c# 2011-10-17

    I added to my c# project an XML file as a Resource file (Resources -> Add Resource (down arrow) add existing file.) I am reading from the file with no problems : static XElement resource; static List<PageType> allPageType; static public List

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.084 (s). 11 q(s)