Some fun with a Netduino Plus and the icndb

A chat with some co-workers about displaying the status of the team’s Jenkins build process led to bit of research into calling RESTful services and JSON support on NetMF devices. Previously this had required a bit of hand crafted code but now it looks like the library support has matured a bit. I don’t run Jenkins at home so I decided to build a NetduinoPlus client for the internet Chuck Norris database which has a RESTful API.

This API returns Chuck Norris “facts”…

“Chuck Norris doesn’t read books. He stares them down until he gets the information he wants.”
“There is no theory of evolution, just a list of creatures Chuck Norris allows to live.”
“Some people wear Superman pajamas. Superman wears Chuck Norris pajamas.”
“Chuck Norris can slam a revolving door.”

The icndb API returns JSON

{ "type": "success", "value": { "id": 109, "joke": "It takes Chuck Norris 20 minutes to watch 60 Minutes.", "categories": [] } }

I used the NetMF system.HTTP libraries to initiate the request and Json.NetMF to unpack the response. This snippet illustrates how I processed the request/response

using (HttpWebRequest request = (HttpWebRequest)WebRequest.Create(@""))
   //request.Proxy = proxy;
   request.Method = "GET";
   request.KeepAlive = false;
   request.Timeout = 5000;
   request.ReadWriteTimeout = 5000;

   using (var response = (HttpWebResponse)request.GetResponse())
      if (response.StatusCode == HttpStatusCode.OK)
         byte[] buffer = new byte[response.ContentLength];

         using (Stream stream = response.GetResponseStream())
            stream.Read(buffer, 0, (int)response.ContentLength);

            string json = new string(Encoding.UTF8.GetChars(buffer));

            Hashtable jsonPayload = JsonSerializer.DeserializeString(json) as Hashtable;

            Hashtable value = jsonPayload["value"] as Hashtable ;

icndb Netduino client

Bill of materials