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.