Click here to Skip to main content
11,926,472 members (55,831 online)
Click here to Skip to main content
Add your own
alternative version


7 bookmarked

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

, 21 Jan 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
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: ""

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)
         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:


...and like so to test an array of JSON elements:


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.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

B. Clay Shannon
Founder "Across Time & Space"
United States United States
Ideaman and Coder at Across Time & Space, creator of "Mark Twain Central" at

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:

You may also be interested in...

Comments and Discussions

-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.151126.1 | Last Updated 21 Jan 2014
Article Copyright 2014 by B. Clay Shannon
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid