In this post I will perform some basic interactions with a RESTful service running on ASP.NET written in C#. I will access the RESTful server using the cURL (a.k.a. curl) command line interface. On a future post I will access the curl API using the C# programming language to illustrate its use.
The simple RESTful service was developed in C# using the Visual Studio Professional 2013 IDE. The RESTful service was based on one of the examples used in a tutorial by Guru 99.
The server exposes a set of tutorial names. The API can be used to display the list of tutorials (GET), get the name of a tutorial associated to a tutorial ID (GET), add a new tutorial (POST) and edit the name of an existing tutorial (PUT).
Let’s start by getting a list of all tutorials in the service by invoking from a command prompt the following:
C:\>curl http://localhost:49496/TutorialService.svc/Tutorial Arrays, Queues, Stacks
By default, curl uses the GET verb and we have specified the address of the service and requested to get a list of all tutorials in the system. At this point in time the list contains: Arrays, Queues and Stacks.
The associated C# code on the server follows:
// **** use the GET verb (list of tutorials) **** [WebGet(UriTemplate="/Tutorial")] // **** use GET verb to fetch all tutorials **** public String GetAllTutorial() { int count = lst.Count(); String tutorialList = ""; for (int i = 0; i < count; i++) { tutorialList = tutorialList + lst[i]; if (i < (count - 1)) { tutorialList += ", "; } } return tutorialList; }
If we are interested in getting the tutorial associated with ID 1 we could issue the following command:
curl http://localhost:49496/TutorialService.svc/Tutorial/1 Queues
The proper name of the tutorial is returned by the call. The associated server code follows:
// **** use GET verb to fetch specified tutorial **** [WebGet(UriTemplate="Tutorial/{tutorialID}")] // **** return the name of the specified tutorial ID **** public String GetTutorialByID (String tutorialID) { int pid; Int32.TryParse(tutorialID, out pid); if ((pid >= 0) && (pid < lst.Count())) { return lst[pid]; } else { return ":o( :o( :o("; } }
Using the following commands we could insert the name of a new tutorial and then check the updated list:
curl -H "Content-Type:application/json" -d "{}" http://localhost:49496/TutorialService.svc/Tutorial/Inter%20Process%20Communications {} curl http://localhost:49496/TutorialService.svc/Tutorial Arrays, Queues, Stacks, Inter Process Communications
The associated C# code in the RESTful service follows:
// **** use POST verb **** [WebInvoke( Method = "POST", RequestFormat = WebMessageFormat.Json, UriTemplate = "/Tutorial/{str}", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)] // **** add a new tutorial to the list **** public void AddTutorial(String str) { if (!lst.Contains(str)) { lst.Add(str); } }
We can edit the name of a tutorial by specifying the ID and the new name as illustrated by the following commands:
curl -X PUT -H "Content-Type:application/json" -d "{}" http://localhost:49496/TutorialService.svc/Tutorial/3/IPC {} curl http://localhost:49496/TutorialService.svc/Tutorial Arrays, Queues, Stacks, IPC
The associated code in the server is as follows:
// **** use PUT verb (update existing tutorial) **** [WebInvoke( Method = "PUT", RequestFormat = WebMessageFormat.Json, UriTemplate = "/Tutorial/{tutorialID}/{tutorialName}", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)] // **** update existing tutorial in the list **** public void EditTutorial(String tutorialID, String tutorialName) { // **** verify the tutorial ID **** int pid; Int32.TryParse(tutorialID, out pid); // **** update the tutorial name (if in list) **** if ((pid >= 0) && (pid <= lst.Count() - 1)) { lst[pid] = tutorialName; } }
Finally, we are able to delete a tutorial from the list by issuing the following commands:
curl -X DELETE -H "Content-Type:application/json" -d "{}" http://localhost:49496/TutorialService.svc/Tutorial/1 {} curl http://localhost:49496/TutorialService.svc/Tutorial Arrays, Stacks, IPC
The associated code in the server follows:
// **** use the DELETE verb **** [WebInvoke( Method = "DELETE", RequestFormat = WebMessageFormat.Json, UriTemplate = "Tutorial/{Tutorialid}", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)] // **** delete the specified tutorial from the list **** public void DeleteTutorial (String tutorialID) { int pid; Int32.TryParse(tutorialID, out pid); if ((pid >= 0) && (pid <= lst.Count() - 1)) { lst.RemoveAt(pid); } }
If you have comments or questions regarding this post, please fee to leave it in the section following this text. I will act on your input as soon as possible.
Enjoy;
John
Follow me on Twitter: @john_canessa