Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

How to Easily Test RESTful Methods with JSON.NET from a Windows Forms Utility

0.00/5 (No votes)
21 Jan 2014 1  
Easily Test RESTful Methods with JSON.NET on Windows Forms

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 ints 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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here