I REST My Case
Although the tip title says "REST methods," and I primarily have ASP.NET Web API REST methods in mind, I reckon this
would work for Web Services, WCF calls, etc. From here on, I will just refer to it all as "REST methods," though.
When you call a REST method, you will either get a scalar value, a single JSON "record" (one element from an array), or an
array of JSON "records" or elements.
When testing a local server you have written, you will have a string base that looks something like:
"http://localhost:28642/api/" Otherwise, if you are testing an already-public REST API (your own or somebody else's), it would
be a little different, such as: "http://www.UMustBNuts.com/ToYou/"
This being the case, you can easily set up a couple of general-purpose methods that you can call with whatever makes a
particular REST call differ from another. First, though, add your base URI global to your unit like so:
private const string BASE_URI = "http://localhost:28642/api/";
Getting Down to Cases
You can have one method that returns a single value (like an int
, a string
, or a JSON element) like this:
private string GetRESTScalarVal(string uri)
{
var client = new WebClient();
return client.DownloadString(BASE_URI + uri);
}
And one that returns an array of JSON objects/elements (that uses JSON.NET) like this:
Preliminary - Don't Be Wary, It's Not Scary
To use the following code snippet, you'll need to add JSON.NET (Newtonsoft.Json) to your project and then add "
using
Newtonsoft.Json
" and "
using Newtonsoft.Json.Linq
;" to your code unit.
And, you will need to have plopped (for your edification, "plopped" is a technical term first coined by either Rick Monday, Tuesday Weld, Wednesday Addams, Sweet Thursday, Joe Friday, Jeff Saturday, Billy Sunday, or Billie Holliday (I need one! (can't you tell?))) a DataGridView
on a Windows form, retaining the default name ("dataGridView1
"). And now (drumroll, por favor!), the code:
private JArray GetRESTData(string uri)
{
var webRequest = (HttpWebRequest) WebRequest.Create(uri);
var webResponse = (HttpWebResponse) webRequest.GetResponse();
var reader = new StreamReader(webResponse.GetResponseStream());
string s = reader.ReadToEnd();
return JsonConvert.DeserializeObject<jarray>(s);
}
private void Popul8TheGrid(string uri)
{
try
{
dataGridView1.DataSource = GetRESTData(BASE_URI + uri);
}
catch (WebException webex)
{
MessageBox.Show(string.Format("Eek, a mousey-pooh! ({0})", webex.Message));
}
}
A Case in Point
Now you simply call these methods like so for a scalar or single value:
MessageBox.Show(GetRESTScalarVal("duckbilledPlatypi/Count"));
...and like so to test an array of JSON elements:
Popul8TheGrid("duckbilledPlatypi/1/42");
Caveat Lector
The calls above assume you have a Controller named duckbilledPlatypiController
that has methods on it that return the
count of data points your Repository provides, and a method that takes two int
s as parameters and returns an IEnumerable
of something (presumably, in this case, of DuckbilledPlatypus
).
Further Evidence
Click the hyperlinks in this sentence for more information on how to build and extend an
ASP.NET Web API extravaganza in the first place.
Two related articles are right here and over here.
I am in the process of morphing from a software developer into a portrayer of Mark Twain. My monologue (or one-man play, entitled "The Adventures of Mark Twain: As Told By Himself" and set in 1896) features Twain giving an overview of his life up till then. The performance includes the relating of interesting experiences and humorous anecdotes from Twain's boyhood and youth, his time as a riverboat pilot, his wild and woolly adventures in the Territory of Nevada and California, and experiences as a writer and world traveler, including recollections of meetings with many of the famous and powerful of the 19th century - royalty, business magnates, fellow authors, as well as intimate glimpses into his home life (his parents, siblings, wife, and children).
Peripatetic and picaresque, I have lived in eight states; specifically, besides my native California (where I was born and where I now again reside) in chronological order: New York, Montana, Alaska, Oklahoma, Wisconsin, Idaho, and Missouri.
I am also a writer of both fiction (for which I use a nom de plume, "Blackbird Crow Raven", as a nod to my Native American heritage - I am "½ Cowboy, ½ Indian") and nonfiction, including a two-volume social and cultural history of the U.S. which covers important events from 1620-2006: http://www.lulu.com/spotlight/blackbirdcraven