Today we are going to simply test a rest based service api using a rest client.
In the previous article we have seen how to create a rest based WCF service application. In this blog I am going to discuss how we can test rest based api using chrome rest client. First let us create a simple wcf application using wcf 4.0 and which we are going to launch in our localhost environment.
Add a wcf project open the Service.svc.cs file, and add a method. I have already explain about how to configure wcf application in previous article.
public class Service : IService { public string GetData(int value) { return string.Format("You entered: {0}", value); } public string GetTodayDate() { return string.Format("You entered: {0}", DateTime.Now.ToShortDateString()); } public string GetItemValue(itemModel model) { if (model != null) { return string.Format("You requested item id: {0}", model.id); } return null; } }
Add a interface for the above service class.
[ServiceContract] public interface IService { // Add your service operations here [OperationContract] [WebInvoke(Method = "GET", UriTemplate = "GetTodayDate", BodyStyle = WebMessageBodyStyle.WrappedResponse, ResponseFormat = WebMessageFormat.Json)] string GetTodayDate(); [OperationContract] [WebInvoke(Method = "POST", UriTemplate = "GetData", BodyStyle = WebMessageBodyStyle.WrappedResponse, ResponseFormat = WebMessageFormat.Json)] string GetData(int value); [OperationContract] [WebInvoke(Method = "POST", UriTemplate = "GetItemValue", BodyStyle = WebMessageBodyStyle.WrappedResponse, ResponseFormat = WebMessageFormat.Json)] string GetItemValue(itemModel model); } // Use a data contract to serealize the object. [DataContract] public class itemModel { public int id { get; set; } public string itemName { get; set; } }
Now run the application and we are ready to test wcf service. Open google chrome and install the RESTClient plugin.
Now to test localhost wcf service open Chrome browser click on Apps button and select to open Advanced Rest Client. Add Request url as wcf service url and select method as GET, click on send button to get the response.
WCF method Get Wrapped Request
Now let us test WCF method having parameter name value.
- Url - http://localhost:9142/Service.svc/GetTodayDate
- Method - GET
- BodyStyle - WebMessageBodyStyle.Wrapped
WCF method Post Wrapped Request
Now let us test WCF method having parameter name value.
- Url - http://localhost:9142/Service.svc/GetData
- Method - Post
- BodyStyle - WebMessageBodyStyle.Wrapped
- Request - {
"value": 02
}
Adding WebInvoke attribute as follows.
[WebInvoke(Method = "POST", UriTemplate = "GetData", BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat = WebMessageFormat.Json)]
WCF method Post WrappedResponse Only Request
Now let us test WCF method passing string/integer parameter.
- Url - http://localhost:9142/Service.svc/GetData
- Method - Post
- BodyStyle - WebMessageBodyStyle.WrappedResponse
- Request - 02
WCF method Post passing Composite type model
Now let us test WCF method passing itemModel in request body.
- Url - http://localhost:9142/Service.svc/GetItemValue
- Method - Post
- BodyStyle - WebMessageBodyStyle.WrappedResponse
- Request - {
"id" : 1,
"itemName": "Product1"
}
Hope you find this post helpful, please share!