Skip to main content

Call POST method RESTful Service using RestSharp in C#

RestSharp dll is a fully loaded class library for the Service calls, it acts as a client for calling services. Internally it handles all the web Request exceptions.

Initially download Restsharp.dll and Add it to the project reference. Call the same dll in your namespace section to access its library. Also add NewtonSoft Json namespace.

In the below example we are passing the CreateRequest object as input, that later converted to Json. Returned string maintains output Json format.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using RestSharp;

namespace ACTService
{
  public class CreateStyle
  {
   public string createStyle(CreateRequest actStyleRequest, string styleCreateUrl)
     {
          var client = new RestClient(styleCreateUrl);
          var request = new RestRequest(Method.POST);
          string jsonRequest = JsonConvert.SerializeObject(actStyleRequest);
          request.AddParameter(
"application/json; charset=utf-8",
jsonRequest,
ParameterType.RequestBody
);
          request.AddBody(jsonRequest);
          var response = client.Execute(request);
          return response.Content.ToString();
       }
   }
}


Comments

  1. IEEE Final Year projects Project Centers in India are consistently sought after. Final Year Students Projects take a shot at them to improve their aptitudes, while specialists like the enjoyment in interfering with innovation. For experts, it's an alternate ball game through and through. Smaller than expected IEEE Final Year project centers ground for all fragments of CSE & IT engineers hoping to assemble. Final Year Projects for CSE It gives you tips and rules that is progressively critical to consider while choosing any final year project point.

    JavaScript Online Training in India

    JavaScript Training in India

    The Angular Training covers a wide range of topics including Components, Angular Directives, Angular Services, Pipes, security fundamentals, Routing, and Angular programmability. The new Angular TRaining will lay the foundation you need to specialise in Single Page Application developer. Angular Training

    ReplyDelete

Post a Comment