The following will show you how to test your wcf rest service from command prompt using CURL tool. And CURL is a command line tool for getting or sending files using URL syntax. For this I have created a simple wcf rest service which contains three method as below:
[IService1]
[ServiceContract(SessionMode = SessionMode.NotAllowed)] public interface IService1 { [WebGet(UriTemplate = "date/{year}/{month}/{day}", ResponseFormat = WebMessageFormat.Xml)] [OperationContract] string GetDate(string day, string month, string year); [WebGet(UriTemplate = "greet", ResponseFormat = WebMessageFormat.Json)] [OperationContract] string Greeting(); [WebInvoke(Method = "POST", UriTemplate = "submit", BodyStyle = WebMessageBodyStyle.WrappedRequest, ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)] [OperationContract] string Save(string data); }
[Service1]
public class Service1: IService1 { public string GetDate(string day, string month, string year) { return new DateTime(Convert.ToInt32(year), Convert.ToInt32(month), Convert.ToInt32(day)).ToString("dddd, MMMM dd, yyyy"); } public string Greeting() { return "Hello World"; } public string Save(string data) { return data + "Has been saved!"; } }
Let’s send request to first method:
[WebGet(UriTemplate = "date/{year}/{month}/{day}", ResponseFormat = WebMessageFormat.Xml)] [OperationContract] string GetDate(string day, string month, string year);
Then go to command prompt and type following code
curl http://localhost:12576/Service1.svc/date/2012/1/12
Let’s send request to second method from curl:
[WebGet(UriTemplate = "greet", ResponseFormat = WebMessageFormat.Json)] [OperationContract] string Greeting();
Then go to command prompt and type following code:
curl http://localhost:12576/Service1.svc/greet
Now,let’s send a post request to third method from command prompt:
[WebInvoke(Method = "POST", UriTemplate = "submit", BodyStyle = WebMessageBodyStyle.WrappedRequest, ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)] [OperationContract] string Save(string data);
curl -H “Content-Type: application/json” -H “Accept: application/json” -X POST -d ‘{“data”:”dummy data”}’ http://localhost:12576/Service1.svc/submit