@manhng

Welcome to my blog!

API Rest: Get + Post + Put + Delete

November 24, 2019 16:18

API Get without Paging (edit)

 Console.OutputEncoding = System.Text.Encoding.UTF8;
RestClient client = new RestClient("http://localhost/");
var request = new RestRequest("api/Values", Method.GET);
IRestResponse response = client.Execute(request);
var responseString = response.Content;
var restResponse = JsonConvert.DeserializeObject<GetResultDTO>(responseString);  

API Get with Paging

Console.OutputEncoding = System.Text.Encoding.UTF8;
RestClient client = new RestClient("http://localhost/");
var request = new RestRequest("api/Values", Method.GET);
Dictionary<string, object> dictionary = new Dictionary<string, object>
{
{ "offset", 0 },
{ "limit", 10 }
};
foreach (KeyValuePair<string, object> item in dictionary)
{
request.AddParameter(item.Key, item.Value, ParameterType.QueryString);
}
IRestResponse response = client.Execute(request);
var responseString = response.Content;
var restResponse = JsonConvert.DeserializeObject(responseString);

API Post

Console.OutputEncoding = System.Text.Encoding.UTF8;
RestClient client = new RestClient("http://localhost/");
var request = new RestRequest("api/Values/", Method.POST);
var item = new CategoryDTO()
{
Id = 1,
Name = "New name",
};
request.AddJsonBody(item);
IRestResponse response = client.Execute(request);
var responseString = response.Content;
Console.WriteLine("Success");
Console.ReadLine();

Categories

Recent posts