We have HTTP webservices that are RPC. They return XML representing the object the either retrieved or created. I want to know the advantages (if any) of "restifying" the services.
- POST http://www.example.com/createDoodad
- GET http://www.example.com/getDoodad?id=13
- GET http://www.example.com/getWidget?id1=11&id2=45
- POST http://www.example.com/createWidget
- POST http://www.example.com/createSprocked
One thing I see is that we don't need representations for EVERY resource and we don't need to support all operations (GET, PUT, POST, DELETE) on all resources either. Basically my question is this.
Convince me that I should be using restful services instead of RPC over HTTP and what would those restful services should be?
-
For one its all about semantics, a URI is a Uniform Resource Indicator. HTTP provides Methods to GET, POST, PUT, DELETE a resource. HTTP headers specify in which format i want to recieve or send the information. This is all readily available through the HTTP protocol.
So you could reuse the same URL you use for HTML output to get XML, JSON in a way that HTTP was meant to be used.
XML-RPC and SOAP are based on calling methods that are described by an XSD or WSDL file whilst REST is based on getting/modifying resources. The difference is subtle but apparent. The URL soully describes the resource and not the action as is often the case with SOAP and XML-RPC.
The benefits of REST are that you can utilize HTTP Verbs to modify a resource as suppose to a Method call that could be named create/new/add etc etc, Meaningful HTTP status codes instead of different kinds of error responses & Being able to specify different formats on the same resource in a standard way.
You also don't have to accept ALL the verbs on a RESTful resource, for example if you want a read-only resource just return a 405 status code
Method Not Allowed
on any verb which isn't GET.Should you redo your RPC calls to REST ? No i dont think so the benefits don't outweigh the development time. Should you learn REST when setting up a new Webservice ? Yes i personally do think so, consuming a REST resource will feel alot more natural and can grow much more rapidly.
EDIT
Why I feel REST wins over XML-RPC/SOAP is that when developing websites you already aggregate all the neccessary data for the output to html, you also write validating code for POST bodies. Why should you change to a different protocol just because the transport markup changes ?
This way when you design a new website (language agnostic) if you really think of URI's as resources you basically use your URI's as method calls with the HTTP Verb prefixing the method call.
I.e A GET on /products/12 with an HTTP Header
Accept: application/json;
basically (imaginary) translates to getProducts(12,MimeType.Json).This 'method' then has to do a couple of things
- Check if we support Json as a mimetype. (Validate Request)
- Validate Request Data
- Aggregate data for product 12.
- Format to JSON and return.
If for some reason in the next 4 year YAML is going to be the next big craze and one of your consumers wishes to talk to you in that way this MimeType is plugged in alot more easy then with regular webservices.
Now product 12 is a resource you most likely also want to accept html mime-types on to display said product but for a URI like /product/12/reviews/14/ you don't need an html counterpart, you just want your consumers to be able to post to that URL to update(PUT)/delete(DELETE) their own review.
In thinking of URI's strictly as resources, not just a location of a web page, and these resources in turn combined with the HTTP request to method invocations on the server side leads to clean (SEO friendly) urls and (more importantly?) ease of development.
I'm sure there are frameworks in any language that will automatically do the mapping of URI's to method invocations for you, I can't recommend one since I usually role out my own.
ASP.NET MVC also works on the same principle but in my opinion doesn't produce RESTful URI's. ASP.NET MVC makes the verb part of the URI by default having said that it's good to note that by no means does ASP.NET MVC force this (or anything for that matter) upon you.
If your going to choose a framework in the very least they should:
- Bind URI's to methods on the server
- Support Object to JSON/XML etc serialization, it's a pain if you have to write this yourself although dependant on the language not neccessary all to difficult.
- Expose some sort of type safe Request helpers to help you determine what was requested without parsing the HTTP Headers manually.
Mike Pone : This is good. Can you comment more on why REST is better for new webservices over XML-RPC?Martijn Laarman : Sure. I feel like I ranted a bit but I hope it explains my position better.Martijn Laarman : I see your a JSP developer, I have heard good stories from Jersy https://jersey.dev.java.net/ form people who've used it to implement REST with JSP. It also supports JAXB to bind XML/JSON requests automatically to objects. -
Try taking the verbs out of your URLs:
- POST http://www.example.com/Doodad
- GET http://www.example.com/Doodad/13
- GET http://www.example.com/Widget/11/45
- POST http://www.example.com/Widget
- POST http://www.example.com/Sprocked
Wahnfrieden : URI construction has nothing to do with REST -
Query strings shouldn't be used for accessing a resource in a hierarchical, non-query manner. Query strings are usually ignored when caching, which is dangerous and slow.