RESTful communication is a great method for light weight communication across systems not requiring the complexity or standards based interoperability of web services. I have used these methods for years with multiple vendors and clients for various CRM related tasks and it worked great. Of course the system supported web services however the need to use them was rare in comparison.
Now it is no longer something used just for B2B based communication but also for standard everyday services such as Twitter. So I will use their REST based API to demonstrate how to read, write and authenticate using REST.
Below is a simple read in order to retrieve the friends timeline for the authenticated user.
public static string GetTimeline(string username, string password)
{
//take the Twitter credentials and Base64 encode them
string encoded_user = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(username + ":" + password));
//create a web request instance for the friends timeline in XML format
HttpWebRequest tweet_request = (HttpWebRequest)WebRequest.Create("http://twitter.com/statuses/friends_timeline.xml");
//add the encoded creadentials to the requests header
tweet_request.Headers.Add("Authorization", "Basic " + encoded_user);
//since we are performing a read the method is set to GET
tweet_request.Method = "GET";
//the call is made and we create an instance of the response’s stream
Stream response_stream = tweet_request.GetResponse().GetResponseStream();
//now we simply convert the XML response into a string that can be
//loaded into an XML document and access the data via XPath
StringBuilder response_builder = new StringBuilder();
int i = 0;
byte[] response_buffer = new byte[8192];
do
{
i = response_stream.Read(response_buffer, 0, response_buffer.Length);
if (i != 0)
{
response_builder.Append(Encoding.ASCII.GetString(response_buffer, 0, i));
}
}
while (i > 0);
response_stream.Close();
return response_builder.ToString();
}
Here we will now write to the service by updating the twitter user’s current status.
public static void Send(string username, string password, string status)
{
//convert data to be written to a byte array
byte[] tweet_bytes = System.Text.Encoding.ASCII.GetBytes("status=" + status);
//Base64 encode the credentials
string encoded_user = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(username + ":" + password));
//create a web request instance for the status update in XML format
HttpWebRequest tweet_request = (HttpWebRequest)WebRequest.Create("http://twitter.com/statuses/update.xml");
//certain web servers have trouble with HTTP requests using Expect-100
tweet_request.ServicePoint.Expect100Continue = false;
//add the encoded credentials to the request header
tweet_request.Headers.Add("Authorization", "Basic " + encoded_user);
//since we are writing we now set the method to POST, the size of data to
//be written and the mime type accordingly
tweet_request.Method = "POST";
tweet_request.ContentType = "application/x-www-form-urlencoded";
tweet_request.ContentLength = tweet_bytes.Length;
//create stream instance from request
Stream request_stream = tweet_request.GetRequestStream();
//finally we write the update to the stream
request_stream.Write(tweet_bytes, 0, tweet_bytes.Length);
request_stream.Close();
}
So as you can see REST calls are the easy part of handling RESTful communication. It is nothing more than standard HTTP calls. It is what you do to and with the data that is the hard part. So now you can see why it is so simple and light weight. Of course I could go into detail describing REST however it is outside the scope of this article and the code pretty much says it all. Enjoy!