I've tried searching but I can't figure out how to access data via a RESTful interface. I'm looking for just example code that shows someone access some data from some imaginary web service using its API. A simple "how-it-works" explanation would be helpful too.
Kind of a hot topic. Expect an explosion of answers ;)
REST works on the principle of using HTTP request methods to determine an application's (the REST server) action on an object. The 4 HTTP methods commonly used are GET, POST, PUT and DELETE.
Say, for example, the object in question is user data. The REST url/object might look something like http://mydomain.com/services/user
If we wanted to get information about existing user, you could GET http://mydomain.com/services/user/someuserid
.
If we wanted to create a user, you would use POST http://mydomain.com/services/user
and the request body would contain the user's information.
If we wanted to change a user's info, you would use PUT http://mydomain.com/services/user/someuserid
. Again, the request body would contain the user's new information.
If we wanted to delete a user, you would use DELETE http://mydomain.com/services/user/someuserid
In summary, the 4 different HTTP methods generally have these meanings, but can differ from server to server, depending on how RESTful it is:
- GET == get, fetch, retrieve, and other synonyms
- POST == create, add
- PUT == change, alter
- DELETE == delete, remove
http://oreilly.com/catalog/9780596529260/
Chapter 2 and 11 - sample code at the website.
If you were looking for an experience similar to what you get with a SOAP-based service and "Add Service Reference" or "Add Web Reference", then you're not going to find it. REST-based services are so lightweight that tools are unnecessary.
You'll just use the WebRequest
class to POST or GET from the service. You'll create the XML to send using LINQ to XML or XML Serialization, or whatever else you like. When the response comes back, you'll parse it, just like any other XML.
As an example, see "A REST Client Library for .NET, Part 1" (sorry, there is no part two).
Check out the Sun Cloud API. It's AFAIK the first (and still one of the only) APIs to embrace the hypermedia as the engine of application state (HATEOAS) constraint in its design and documentation. This seemingly minor constraint turns out to be one of the central ideas of REST and it has been consistently ignored over the last few years.
The Sun Cloud documentation has some nice examples of sample requests, responses, and what hypertext-driven media types might look like.
Here is some pseudo code that I just used to answer a similar question.
The general flow of any http based RESTful client inquiry should go something like this:
- Do a HTTP GET on the root url of the API.
- Parse the response based on the media type specified in the http header "Content-Type".
- Does the response contain the answer to my question?
- If yes then extract the information and do what you want with it.
- If no, then does the response contain a link to another resource that may have the answer to my question.
- If yes then do a HTTP GET or POST on that link based on what the media type definition tells you to do. Goto step 3.
- If no then stop looking and tell the user you cannot find an answer.
After a little more looking, I found something that gave me the information I needed. http://developer.yahoo.com/php/howto-reqRestPhp.html