Brief Discussion About RestHelper?Why We Have to Go For Rest-client?
REST web services have become mainstream and it is important as a developer to know how to communicate with the onslaught of services built using this architecture which now flood our industry. In this article I will provide you with a module I developed for making web request to REST services using C# and give you some details on how the code works.
1.Firstly you have To add
a.Restsharp
b.Newtonsoftjson From nuget packages.
After that u have to add a RestHelper class(Resthelper.cs)
and then call in ur required place.
RestHelper
public class RestHelper
{
public static IRestResponse PostBasicAuthenication(string baseUrl, string resourceUrl, string userName, string password, string json)
{
var client = new RestClient(baseUrl);
client.Authenticator = new HttpBasicAuthenticator(userName, password);
var request = new RestRequest(resourceUrl, Method.POST);
request.AddParameter("text/json", json, ParameterType.RequestBody);
request.RequestFormat = DataFormat.Json;
return client.Execute(request);
}
public static IRestResponse Post(string baseUrl, string resourceUrl, string json)
{
var client = new RestClient(baseUrl);
var request = new RestRequest(resourceUrl, Method.POST);
request.AddParameter("text/json", json, ParameterType.RequestBody);
request.RequestFormat = DataFormat.Json;
return client.Execute(request);
}
public static string ConvertObjectToJason<T>(T arg)
{
return JsonConvert.SerializeObject(arg);
}
public static IRestResponse Get(string baseUrl, string resourceUrl, List<List<string, string>> paraMeters)
{
string parameters = null;
if (paraMeters.Any())
{
parameters = paraMeters.Aggregate(parameters, (current, param) => current + (param.Name + "=" + param.Value + "&"));
parameters = parameters.Trim('&');
}
var client = new RestClient(baseUrl);
RestRequest request;
if (parameters == null)
{
request = new RestRequest(resourceUrl, Method.GET);
}
else
{
request = new RestRequest(resourceUrl + "?" + parameters, Method.GET);
}
request.RequestFormat = DataFormat.Json;
return client.Execute(request);
}
public static IRestResponse Get(string baseUrl, string resourceUrl)
{
var client = new RestClient(baseUrl);
RestRequest request;
request = new RestRequest(resourceUrl, Method.GET);
request.RequestFormat = DataFormat.Json;
return client.Execute(request);
}
public class List<T, D>
{
public T Name { get; set; }
public D Value { get; set; }
}
//public static ResponseObject GetResponseObject(IRestResponse response)
//{
// return JsonConvert.DeserializeObject<ResponseObject>(response.Content);
//}
}
Program.cs
public class Program
{
static void Main(string[] args)
{
var address = new Address();
address.AddressLabel = "Jill's Address";
address.City = "Rock Island";
address.Country = "US";
address.State = "IL";
address.Street = "123 Main";
address.Zip = "61201";
var json = RestHelper.ConvertObjectToJason(address);
try
{
object obj = address;
string url = ConfigurationManager.AppSettings["IdentityBean"].ToString();
var response = RestHelper.Post(url, "api/journal/updateposting", json);
if (response.ErrorMessage != null)
{
}
}
catch (Exception ex)
{
var message = ex.Message;
}
}
}
public class Address
{
public string AddressLabel { get; set; }
public string City { get; set; }
public string Country { get; set; }
public string State { get; set; }
public string Street { get; set; }
public string Zip { get; set; }
}
Comments
Post a Comment