Live data from Hacker News

Designing a RESTful Web Application

quandyfactory.com

11–20 of 24 posts

Re: Designing a RESTful Web Application

#11
post #8

"For example, if you want to add the ability to create an article, it might be tempting to create a URI called /create_article/. This is wrong, because it conflates the object (the resource) and the action (creation)." So what URL do I use if I want to search articles? What if I want to approve articles? The simple CRUD perspective has fallen apart for me pretty quickly in practice- I need more than 6 verbs (adding H…

I've always thought that the URL to use when searching articles is the same you would use when retrieving a collection of articles, just with the search terms or filters included as parameters, ie, /articles?q=terms Approving articles could be as simple as posting TRUE to the "approved" attribute of an article, ie, /articles/1234/approved You just need to make sure that every resource that can be modified or retrieve…

Or treat search as a first-class resource.

Re: Designing a RESTful Web Application

#13

"For example, if you want to add the ability to create an article, it might be tempting to create a URI called /create_article/. This is wrong, because it conflates the object (the resource) and the action (creation)." So what URL do I use if I want to search articles? What if I want to approve articles? The simple CRUD perspective has fallen apart for me pretty quickly in practice- I need more than 6 verbs (adding H…

To search use the search resource. Then you have, resource: "search results containing the word 'tuesday'"; url: /search?q=tuesday

To approve you create a new approval resource: POST to /approval with the id of the resource to approve. This creates a new resource at /approval/1

Then you can revoke the approval by DELETE /approval/1

Re: Designing a RESTful Web Application

#14
"This collision between the inclination to regard PUT as creating and POST as updating is a significant source of confusion about how to well-design a RESTful system, and deserves more attention."

I like this text for explicitly bringing this problem up.

Re: Designing a RESTful Web Application

#15

"For example, if you want to add the ability to create an article, it might be tempting to create a URI called /create_article/. This is wrong, because it conflates the object (the resource) and the action (creation)." So what URL do I use if I want to search articles? What if I want to approve articles? The simple CRUD perspective has fallen apart for me pretty quickly in practice- I need more than 6 verbs (adding H…

> search articles?

GET /search?q=terms

> What if I want to approve articles?

POST /articles/articleID?approved=1

It's actually got potential to be very clean.

Re: Designing a RESTful Web Application

#17
post #8

Earlier quoted context omitted.

I've always thought that the URL to use when searching articles is the same you would use when retrieving a collection of articles, just with the search terms or filters included as parameters, ie, /articles?q=terms Approving articles could be as simple as posting TRUE to the "approved" attribute of an article, ie, /articles/1234/approved You just need to make sure that every resource that can be modified or retrieve…

Or treat search as a first-class resource.

I wonder if it makes more sense to treat search as a first class method, i.e. issue an HTTP SEARCH request against a resource.

Re: Designing a RESTful Web Application

#18
Regarding response data formats, xhtml has the advantage of being both html (a format browsers know how to handle) and xml (a format that is well structured and parsable.)

Semantically, xhtml can represent all of the same data structures/types as JSON: lists are UL elements, dictionaries are OL elements, numbers, strings, and booleans are really just strings in both, objects are really just dictionaries, and nulls in xhtml can be a special string as in JSON or an empty element.

In both cases, the client has to either already know about the data structure and how to navigate it, or can treat it generically. XHTML has an advantage here; every data item can be labeled with an id or class attribute to make it easier to find. You can't do that in JSON unless everything is a dictionary value.

For methods, browsers support GET and POST, and they can simulate PUT with upload controls. For DELETE, the common approach is to use POST with _method=DELETE as a parameter. That's easy to adjust in your service as the request comes in so most of your service doesn't have to know about it.

Browsers are also pretty good about sending other headers, particularly the ones that have to do with caching. That'll force you to deal with them, which is a good thing.

Ideally, you'll want to just generate a data structure internally, and use the Accept request header to determine whether to send a text/json or text/html response. That way clients that want JSON can ask for and get it, while generic clients (eg: browsers) will get something they can handle. You'll also be generating the xhtml programatically, which will help to keep it uniform and predictably structured.

Re: Designing a RESTful Web Application

#19

Earlier quoted context omitted.

Or treat search as a first-class resource.

I wonder if it makes more sense to treat search as a first class method , i.e. issue an HTTP SEARCH request against a resource.

That could be done; WebDAV introduced several new methods to HTTP. The drawback is that your clients lose the ability to use standard HTTP libraries to talk to your service because you're using a non-standard set of methods. The 'Uniform' in URI and URL doesn't just apply to the resource identifiers; in a RESTful system the methods and representations both need to be uniform as well. That allows general-purpose clients to be able to interact with the service, and that was what made the web take off when earlier FTP and Gopher systems did not.

Re: Designing a RESTful Web Application

#20

"For example, if you want to add the ability to create an article, it might be tempting to create a URI called /create_article/. This is wrong, because it conflates the object (the resource) and the action (creation)." So what URL do I use if I want to search articles? What if I want to approve articles? The simple CRUD perspective has fallen apart for me pretty quickly in practice- I need more than 6 verbs (adding H…

To search use the search resource. Then you have, resource: "search results containing the word 'tuesday'"; url: /search?q=tuesday To approve you create a new approval resource: POST to /approval with the id of the resource to approve. This creates a new resource at /approval/1 Then you can revoke the approval by DELETE /approval/1

A search resource generally isn't going to be a good idea in a RESTful design. What does it search? Your service will typically contain more than just one set of resources, so you'd end up either creating a separate search resource for each set or passing set identifiers of some sort as a query parameter to the search resource.

The typical model is to have 'container' resources. For example, if /articles/9001 is a resource, /articles is the container. You can POST to the container to create a new resource, and you can GET the container to find out some information about it, such as the number of resources it contains and a list of them. For this part of the design I'll typically return a list of links to the first N resources in the container and a navigation link to the next N resources. That navigation link uses the same URL for the container resource, but with a query parameter that tells it which resource to start on. I'll typically also implement search/filter query parameters to allow further control over the list of resources to include in the container's representation.

Post reply on HN