Kailash's Blogs

Post JSON data to WCF REST service C#

Kailash Chandra Behera | Wednesday, June 24, 2020
Kailash's Blogs


Post JSON data to WCF REST service C# provides code example to call a post method of WCF REST service. It also provide the reference of third party libraries which can be used to convert C# object to Json string.

Post JSON data to WCF REST service C#

Getting Started

The below code example will pass json string data to rest service post method using c#. Here I have used the third party library to send json to wcf service.

To generate a JSON string, There are multiple options are available. For example, you can use the DataContractJsonSerializer class which is available in System.Runtime.Serialization.Json namespace or you use the Newtonsoft JsonConverter. As Newtonsoft is an easy way to convert domain object to JSON string, hence we will go with Newtonsoft.

In the below i have give example of Newtonsoft Json converter, see it for more details or visit the site.

 Product product = new Product();  
 product.Name = "Apple";  
 product.ExpiryDate = new DateTime(2008, 12, 28);  
 product.Price = 3.99M;  
 product.Sizes = new string[] { "Small", "Medium", "Large" }  
 string output = JsonConvert.SerializeObject(product);  

Code Example of Post JSON data to WCF REST service C#

The below example provides sample code which calls restful API using RestClient class.

       IRestRequest request = new RestRequest(Method.POST);  
       request.AddHeader("content-type", "application/json");  
       request.RequestFormat = DataFormat.Json;  
       request.AddParameter("Parameter_Name", "Parameter_Value", ParameterType.RequestBody);  

       RestClient client = new RestClient("url");  

       IRestResponse response = client.Execute(request);
       string responsevalue = response.Content;  

Post JSON data to WCF REST service C#

In the above code change the parameter name whatever you want and replace the "Parameter_Value" with your JSON string object. Here you need not worry about the response. The library will automatically convert the response value to JSON string.

Download RestClient to Post JSON data to WCF REST service C#

The RestClient class is one of best option for consuming rest full services or APIs. it has implemented the logic internally to get json response from wcf service.

It is third party library provided by RestSharp and available in NuGet Manager. To consume this library minimum NETFramework 2.0 and later version is required.

Reference to RestClient library can be added directly from NuGet Manager in Microsoft Visual Studio. In NuGet Manager, in Browser just search for RestSharp the latest build will be available in the search list. Currently the latest version 106.6.1 is available

If you are not able to search the library, you can directly use below command in NuGet Package Manager Console.

 dotnet add package RestSharp --version 106.6.10  

Related Articles

  1. Getting querystring value using Javascript
  2. Add new Row into HTML Table Using JQuery and Javascript
  3. Export HTML Table to Excel Using JavaScript


In the above of this blog, We discussed the library used to convert object to JSON string, code example of RestClient to Post JSON data to WCF REST service C# and how to download RestClient library from NuGet. I hope you have enjoyed it a lot.