Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you implement resource "edit" forms in a RESTful way?

Tags:

rest

We are trying to implement a REST API for an application we have now. We want to expose read/write capabilities for various resources using the REST API. How do we implement the "form" part of this? I get how to expose "read" of our data by creating RESTful URLs that essentially function as method calls and return the data:

GET /restapi/myobject?param=object-id-maybe

...and an XML document representing some data structure is returned. Fine.

But, normally, in a web application, an "edit" would involve two requests: one to load the current version of the resources and populate the form with that data, and one to post the modified data back.

But I don't get how you would do the same thing with HTTP methods that REST is sort of mapped to. It's a PUT, right? Can someone explain this?

(Additional consideration: The UI would be primarily done with AJAX)

-- Update: That definitely helps. But, I am still a bit confused about the server side? Obviously, I am not simply dealing with files here. On the server, the code that answers the requests should be filtering the request method to determine what to do with it? Is that the "switch" between reads and writes?

like image 723
Sam McAfee Avatar asked Sep 05 '08 15:09

Sam McAfee


People also ask

Which REST method is used for updating the resources?

As a RESTful API HTTP method, PUT is the most common way to update resource information.

Which RESTful method is used create a resource on the server?

In conclusion, we should use the POST method to create a new resource, and the PUT method to update an existing resource.


2 Answers

There are many different alternatives you can use. A good solution is provided at the microformats wiki and has also been referenced by the RESTful JSON crew. As close as you can get to a standard, really.

 Operate on a Record

GET /people/1
    return the first record 
DELETE /people/1
    destroy the first record 
POST /people/1?_method=DELETE
    alias for DELETE, to compensate for browser limitations 

GET /people/1/edit
    return a form to edit the first record 
PUT /people/1
    submit fields for updating the first record 
POST /people/1?_method=PUT
    alias for PUT, to compensate for browser limitations 
like image 66
Alexandros Marinos Avatar answered Sep 29 '22 18:09

Alexandros Marinos


I think you need to separate data services from web UI. When providing data services, a RESTful system is entirely appropriate, including the use of verbs that browsers can't support (like PUT and DELETE).

When describing a UI, I think most people confuse "RESTful" with "nice, predictable URLs". I wouldn't be all that worried about a purely RESTful URL syntax when you're describing web UI.

like image 20
Brad Wilson Avatar answered Sep 29 '22 17:09

Brad Wilson