"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.
"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 HTTP OPTIONS and HEAD in there). I think HATEOAS is a pretty good approach, but there still seems to be a lot of noun-ification going on in RESTful apps just to stick with the style. (e.g, PUT //article/1234/approval used when disapproving an article)
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 retrieved has a URL.
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.
>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.)
And where is the advantage in that over JSON? It's just a Javascript object and browsers know very
well how to handle it. That's the point.
>Semantically, xhtml can represent all of
>the same data structures/types as JSON
XHTML has HTML semantics, i.e. it's intended to be used to mark the structure of the text. It has no advantage
for a general purpose data exchange.
>dictionaries are OL elements
Nope. Or did you think about DL/DT/DD? Still nope.
>numbers, strings, and booleans are really just strings in both
Nope. In JSON only strings are strings. Numbers, booleans and null are Javascript numbers, booleans and null.
>objects are really just dictionaries
Objects are objects. Entire JSON response is an object, you can assign it to variable and use straight away.
>and nulls in xhtml can be a special string as in JSON or an empty element.
As I said, nulls in JSON are Javascript nulls, not "some special string".
>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.
Uhm. If we are talking about data in key-value then JSON has a huge advantage there. Not only you can do it in JSON, but
you do exactly that and with very little overhead—that's exactly what makes JSON so attractive and popular.
var article = {"title":"Adventures in JSON land", "authors":[{"name":"Jane Doe"}], "published":false, "tags":["JSON", "Javascript", "data format"]}.
That's it. Want to access your data? It's right there:
The author seems to get confused himself about PUT vs. POST. At one point he says POST is used to create resources, and at another point he says PUT matches up roughly with SQL's INSERT (ignoring idempotence).
> It's tempting to assume that the HTTP verbs line up nicely with the SQL CRUD verbs: [...] They're superficially similar but they're not identical, and treating them as such leads to this kind of gotcha.
[+] [-] troels|15 years ago|reply
I like this text for explicitly bringing this problem up.
[+] [-] fi0660|15 years ago|reply
http://roy.gbiv.com/untangled/2008/rest-apis-must-be-hyperte...
[+] [-] danielharan|15 years ago|reply
[+] [-] mattmcknight|15 years ago|reply
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 HTTP OPTIONS and HEAD in there). I think HATEOAS is a pretty good approach, but there still seems to be a lot of noun-ification going on in RESTful apps just to stick with the style. (e.g, PUT //article/1234/approval used when disapproving an article)
[+] [-] carpo|15 years ago|reply
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 retrieved has a URL.
[+] [-] daredevildave|15 years ago|reply
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
[+] [-] ErrantX|15 years ago|reply
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.
[+] [-] BerislavLopac|15 years ago|reply
[+] [-] DougWebb|15 years ago|reply
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.
[+] [-] rimantas|15 years ago|reply
var article = {"title":"Adventures in JSON land", "authors":[{"name":"Jane Doe"}], "published":false, "tags":["JSON", "Javascript", "data format"]}.
That's it. Want to access your data? It's right there:
Now do the same in XHTML and you will have much better understanding what advantages of JSON are.[+] [-] rmk|15 years ago|reply
[+] [-] jsharpe|15 years ago|reply
[+] [-] _sh|15 years ago|reply
[+] [-] RyanMcGreal|15 years ago|reply
[+] [-] unknown|15 years ago|reply
[deleted]
[+] [-] nkohari|15 years ago|reply
[+] [-] RyanMcGreal|15 years ago|reply
[+] [-] w3matter|15 years ago|reply
[deleted]